to be more specific, I have DateTime? BirthDate
my question is what will happen in this case:
lblBirthDate.Text = myObject.BirthDate.GetValueOrDefault().ToString();
if BirthDate will be null. I have resharper installed, and it doesn’t warn me that I could have a null problem there…
If
BirthDateisnull, you get the localized string representation of the default instance ofDateTime. That is, you’ll get the string representation of 00:00:00.0000000, January 1, 0001.You don’t get a warning from ReSharper because you don’t have a null problem there. It is not a runtime exception to call
Nullable<T>.GetValueOrDefaulton a null instance of aNullable<T>. Instead, you get the default value if you have a null instance. That’s exactly whatGetValueOrDefaultmeans: getValueifHasValueistrue, otherwise get the default value for the underlying struct type.