开发者

Why is there a questionmark on the private variable definition?

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 ques开发者_开发百科tion 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜