简体   繁体   English

如果默认值为null并转换为字符串会发生什么

[英]what happens if default value is null and convert to string

to be more specific, I have DateTime? 更具体地说,我有DateTime吗? BirthDate 生日

my question is what will happen in this case: 我的问题是在这种情况下会发生什么:

lblBirthDate.Text = myObject.BirthDate.GetValueOrDefault().ToString();

if BirthDate will be null. 如果BirthDate将为null。 I have resharper installed, and it doesn't warn me that I could have a null problem there... 我已经安装了Resharper,但并没有警告我那里可能存在null问题...

If BirthDate is null , you get the localized string representation of the default instance of DateTime . 如果BirthDatenull ,则将获取DateTime的默认实例的本地化字符串表示形式。 That is, you'll get the string representation of 00:00:00.0000000, January 1, 0001. 也就是说,您将获得0001年1月1日00:00:00.0000000的字符串表示形式。

You don't get a warning from ReSharper because you don't have a null problem there. ReSharper不会向您发出警告,因为那里没有空问题。 It is not a runtime exception to call Nullable<T>.GetValueOrDefault on a null instance of a Nullable<T> . 这不是一个运行时异常调用Nullable<T>.GetValueOrDefault上的空实例Nullable<T> Instead, you get the default value if you have a null instance. 相反,如果您具有空实例,则将获得默认值。 That's exactly what GetValueOrDefault means: get Value if HasValue is true , otherwise get the default value for the underlying struct type. 这就是GetValueOrDefault含义:如果HasValuetrue ,则获取Value ;否则,获取底层结构类型的默认值。

I'm pretty sure GetValueOrDefault returns DateTime.MinValue in the case of null. 我很确定GetValueOrDefault在为null的情况下返回DateTime.MinValue

Here's a few ways you can handle null values: 您可以通过以下几种方式处理空值:

DateTime? myNullDate = null;

DateTime myDate;
myDate = myNullDate ?? DateTime.Now; // myDate => DateTime.Now
myDate = myNullDate ?? default(DateTime); // myDate => DateTime.MinValue
myDate = myNullDate.GetValueOrDefault(DateTime.Now); // myDate => DateTime.Now
myDate = myNullDate.GetValueOrDefault(); // myDate => DateTime.MinValue

Resharper won't show you any error because the default value for DateTime is 1/1/1 00:00:00. Resharper不会显示任何错误,因为DateTime的默认值为1/1/1 00:00:00。 You can get the default value for any type using the default operator: 您可以使用默认运算符获取任何类型的默认值:

DateTime defaultDateTime = default(DateTime);

This value is exactly the same as DateTime.MinValue 此值与DateTime.MinValue完全相同

If you need to use nulls, you must declare your property as nullable: 如果需要使用空值,则必须将属性声明为可空值:

DateTime? BirthDay { get; set; };

The nullable DateTime DateTime? 可为空的DateTime DateTime? supports calling ToString() without error even if the value is null, giving an empty string if so. 即使值是null,也支持无错误地调用ToString()如果是,则提供一个空字符串。 Then you don't have to worry about nulls. 然后,您不必担心null。

Is much more consistent using a nullable DateTime than having a special value to represent missing values, like default(DateTime) , ie DateTime.MinValue; 使用可空的DateTime比使用特殊值来表示缺失值(例如default(DateTime) ,即DateTime.MinValue; default(DateTime)具有更大的一致性DateTime.MinValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM