比较nullable int inside if if not not throw

时间:2018-04-13 12:20:30

标签: c#

比较if语句中的nullable int似乎有效,即使它显然是空值。下面的代码示例演示了该场景:

List<string> list = null;

var count = list?.Count; //null

if(count > 0) // no complaint, just evaluate to false
{
  Console.WriteLine("greater");
}

if(count == null)
{
  Console.WriteLine("wow! null < 0"); // works like charm
}

从上面看,if语句似乎没有抛出,而是评估为false。我能相信它总是如此,或者我错过了什么?

0 个答案:

没有答案