Possible Duplicate:
C# okay with comparing value types to null
If I try to assign null
to a non-nullable type in C#:
System.DateTime time = null;
I'll get a compile-time error:
error CS0037: Cannot convert null to 'System.DateTime' because it is a non-nullable value type
which makes sense. But if compare the same type against null
:
System.DateTime time = obtainFromSomewhere();
if( time == null ) {
//whatever;
}
there's no compile-time error. This doesn't make sense to me - if I can't assign null
then why would it ever be null
?
Why am I allowed to compare a non-nullable type with null
?