C#Nullable为什么是object.HasValue如果object为null?

时间:2017-09-10 11:29:42

标签: c# nullable

为什么是这条线:

Assert.IsFalse(myInt.HasValue);

可能?我希望NullPointerException myIntnull

    [TestMethod]
    public void Test_NullableInt_ShortDeclaration_WithValueAssignment()
    {
        int? myInt = null;

        Assert.IsNull(myInt);
        Assert.IsFalse(myInt.HasValue); // Why is this possible?

        myInt = 5;
        Assert.IsNotNull(myInt);
        Assert.IsTrue(myInt.HasValue);
    }

此致

0 个答案:

没有答案