【问题标题】:Xamarin Studio c# null propagating operatorXamarin Studio c# null 传播运算符
【发布时间】:2017-11-15 07:28:58
【问题描述】:

在填写有关 Xamarin Studio 的错误案例之前,我想征求您的意见

public class Class1
{
    public int AProp { get; set; }
}

考虑这个简单的场景

Class1 c;
var aProp = c?.AProp;

不应该将 aProp 推断为 int? 实例,因为它在 Visual Studio 2015+ 上的 c#-6.0 中?因为实际上不是,所以推断为普通的int

Xamarin Studio 没有抱怨运算符,但它没有将 aProp 识别为可空类型,因此抱怨 .HasValue 属性评估;不仅是 Intellisense,而且在编译时更糟糕

是我遗漏了什么还是只是我的 IDE?

编辑: Acutally 我刚刚发现我可以在空合并检查中使用它,即使推断的类型实际上是 int! 什么混合!呵呵

【问题讨论】:

  • 你能显示你使用HasValue的代码吗?
  • 只是为了好奇,类型是什么?
  • @rory.ap 最简单的你可以想象...只需将类放在命名空间中,在其中添加任何方法,调用第二两行并添加类似if (aProp.HasValue) { } 的内容。结果:编译错误'int' does not contain a definition for 'HasValue' [...]
  • 嗯...没关系。解释一下:你说它应该是int?,但它不是。那么,那是什么?我假设它被解释为int。我没有看到你明确提到这一点,(尽管你在评论中提到了)。
  • Xamarin Studio 6.3 版构建 863 和 VS2017 都将 c?.AProp 推断为 int?

标签: c# nullable xamarin-studio c#-6.0 null-propagation-operator


【解决方案1】:

我已在 Visual Studio 编译的新控制台应用程序中尝试了此代码,并且运行良好:

public class Foo
{
    public int Bar { get; set; }
}

static void Main(string[] args)
{

    Foo foo = new Foo(); //same result with Foo foo = null;

    var baz = foo?.Bar;
    if (baz.HasValue) //Expected Error here, but compiles fine
    {
        throw new NotImplementedException();
    }
}

所以这绝对是一个错误。您应该在 Xamarin Studio 的 Bug 跟踪器中打开一个新问题。

【讨论】:

  • 谢谢,但这个错误实际上在我的脑海中! XD 正如你在我最后一次 OP 的 cmets 中看到的那样
  • 好的。我会去医院或杀虫剂然后?头部的虫子并不总是健康的?
  • lol 04.35 这里是一个炎热的下午,空调坏了......我想我只需要接受他们 XD
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 2019-04-25
  • 2017-12-09
  • 1970-01-01
  • 2015-02-26
  • 2015-12-07
  • 1970-01-01
  • 2016-03-14
相关资源
最近更新 更多