Why is this a valid comparison

Here is a sample code:

static DateTime time; if (time == null) { /* do something */ } 

Since DateTime cannot be null, why is this code compiling?

Edit: The problem is not only that this code will always return false, but for some reason like DateTime , which is never null, is allowed with this comparison.

+6
source share
1 answer

Although time is a type that is not nullable, it can be converted to NULL and compared to null . Comparison will give false , which is the correct result.

This does not mean, however, that this is good code. Tools like re: sharpper will flag this line with a warning: "Expression is always false."

+6
source

Source: https://habr.com/ru/post/985780/


All Articles