DEV Community

loading...

Discussion on: The perfect non-null test

Collapse
peledzohar profile image
Zohar Peled Author

the fact that a type is non-nullable doesn't prevent you from asking if it equals null. Remember that == (and !=) can be overloaded and this means that you could have a struct with == and != overloads that specially treat null.
The is operator, on the other hand, can't be overloaded - so the compiler can and will issue an error when attempting to check if a non-nullable type is null.

If you add if(dt is object) you should be getting true every time, since dt is non-nullable.