Why is there a questionmark on the private variable definition?

我是研究僧i 提交于 2019-11-30 13:41:14

问题


I am reading an article about the MVVP Pattern and how to implement it with WPF. In the source code there are multiple lines where I cannot figure out what the question marks in it stand for.

private DateTime? _value;

What does the ? mean in the definition? I tried to find it in the help from VS but failed.


回答1:


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.




回答2:


That means the type is Nullable.




回答3:


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



回答4:


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




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/2326158/why-is-there-a-questionmark-on-the-private-variable-definition

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