0

Comparing a nullable int inside an if statement seems to work even though it's clearly a null value. The code example below demonstrates the scenario:

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
}

From the above, it seems the if statement does not throw, but rather evaluate to false. Can I trust that it will always be the case or am i missing something?

rethabile
  • 3,029
  • 6
  • 34
  • 68

0 Answers0