开发者

Explanation of int? vs int [duplicate]

This question already has answers here: 开发者_高级运维 Closed 11 years ago.

Possible Duplicate:

What's the difference between 'int?' and 'int' in C#?

I've come across some code in C# that declares a variable as: int? number

What does the ? mean and how does this differ from just: int


int cannot be null.

int? is an alias for the Nullable<int> struct, which you can set to null.


int? is a shorthand for creating an instance of the generic System.Nullable<T> structure type. It allows you to make your variable nullable. Remember, given that the <ValueType>? syntax is a shorthand, you could declare your variables thus:

Nullable<int> i = 10;


int? is shorthand for Nullable<int> which allows you to pretend that an integer can handle nulls.

int? foo = null;

It is useful for indicating a lack of value where you would previously have used a magic value (-1) in the past, and also useful when dealing with database columns that allow null entries.

For a quasi-in-depth look, a Nullable<T> (introduced in .NET 2.0) is simply a wrapper over a value type T that exposes two properties, HasValue and Value, where HasValue is a boolean that indicates if the value has been set and Value (obviously enough) returns the value. It is an error to access Value if HasValue is false. Therefore, to access Value, it is good form to check HasValue first. Additionally, if you simply want to normalize any non-values to default values (0 for numeric types), you can use the method GetValueOrDefault() without needing to check HasValue.

Note that although you appear to set foo to null, it's not actually null under normal usage scenarios. null is simply additional syntactic sugar for this type. The above line of code translates to

Nullable<int> foo = new Nullable<int>();

Initializing the variable in this fashion simply sets the HasValue property to false.

However, in situations involving boxing, the value will actually box to null if HasValue is false (it will otherwise box to T). Be aware of the consequences! For example, in:

int? foo = null; 
string bar = foo.ToString(); // this is fine, returns string.Empty
Type type = foo.GetType(); // blows up! GetType causes the value to box
                           // resulting in a NullReferenceException

That's a quick crash course. For more, visit the documentation.


It's syntactic compiler sugar for Nullable<int>

Basically your number (or any other value type) can be null as well as it's value. You check for a value using the HasValue property. They can be cast into their value types (although this will fail if they're null) or you can use the Value property (again it will throw an exception if it is null)

One thing which usually appears to be overlooked when using nullable types is the GetValueOrDefault() method which returns default(T) if the object is null.

As @Kyle Trauberman points out in the comment you can indeed compare it to null instead of checking HasValue. The type itself is a value type with overriden equality methods so that much as it will never be null itself it will return true when compared to null if it doesn't have a value.


A questionmark behind the declaration means that the variable is nullable.


int? can be null where as int can not.

Reference Nullable Types: http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=VS.100).aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜