Xamarin Studio c#null传播运算符

时间:2017-06-13 13:22:54

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

在填写关于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属性评估,例如;不只是使用智能感知,而是在编译时更糟糕的是

我错过了什么,还是仅仅是我的IDE?

修改: 实际上我发现我可以在空合并检查中使用它,即使推断类型实际上是int !! 什么混合! XD

1 个答案:

答案 0 :(得分: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跟踪器中打开一个新问题。