Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Nullable DateTime work correct?

Tags:

c#

datetime

Please check following code

DateTime? tmp = new DateTime();
tmp = null;
return tmp.ToString();

It returns String.Empty.

Is it correct?

May be it will be better to rise exception in second line of code

like image 258
Andrei Andrushkevich Avatar asked Dec 28 '10 15:12

Andrei Andrushkevich


1 Answers

Yes, it's correct. From the documentation

The text representation of the value of the current Nullable<T> object if the HasValue property is true, or an empty string ("") if the HasValue property is false.

Note also that Nullable<T>.Equals and Nullable<T>.GetHashCode do not throw in this case but that Nullable<T>.GetType does throw. This is because Object.Equals, Object.GetHashCode and Object.ToString are overridden for Nullable<T> but that Object.GetType is not (because it can not be as it is not marked as virtual).

like image 136
jason Avatar answered Oct 12 '22 00:10

jason