Why is there a questionmark on the private variable definition?

蓝咒 提交于 2019-11-30 11:15:14

It's a nullable value. Structs, by default, cannot be nullable, they must have a value, so in C# 2.0, the Nullable<T> type was introduced to the .NET Framework.

C# implements the Nullable<T> type with a piece of syntactic sugar, which places a question mark after the type name, thus making the previously non-nullable type, nullable.

That means the type is Nullable.

cannot be null

DateTime                        
DateTime dt = null;   // Error: Cannot convert null to 'System.DateTime'
                         because it is a  non-nullable value type 

can be null

DateTime? / Nullable<DateTime>  
DateTime? dt = null;  // no problems

This is a nullable type, you can assign null to it

It means that the field is a Nullable<DateTime>, i.e. a DateTime that can be null

Private DateTime? _value - means that the _value is nullable. check out this link for a better explanation.

http://davidhayden.com/blog/dave/archive/2005/05/23/1047.aspx

Hope this helps.

Thanks, Raja

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!