Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of int? vs int [duplicate]

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

like image 643
5n0u7 Avatar asked Jun 17 '11 17:06

5n0u7


People also ask

What is the difference between int and int?

Answers. int is C#'s alias for the System. Int32 datatype, and represents a 32-bit signed integer. int?, on the other hand, is the shortcut way of saying Nullable<int>.

Is it better to use int or integer in Java?

int provides less flexibility as compare to Integer as it only allows binary value of an integer in it. Integer on other hand is more flexible in storing and manipulating an int data. Since Wrapper classes inherit Object class, they can be used in collections with Object reference or generics.

What type is int []?

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.

Can we write integer instead of int in C?

In the C programming language, the keyword 'int' is used in a type declaration to give a variable an integer type. However, the fact that the type represents integers does not mean it can represent all integers.

What is the difference between int[] a and int[] a?

int [,] a is a two dimensional array like a grid with a fixed amount of rows and columns. int [] [] a is a jagged array, an array of arrays where the sub arrays can for instance have different lengths. what is the difference between int [] [] and int [,]? int [,] a is a two dimensional array like a grid with a fixed amount of rows and columns.

What is an int[][] array?

int [] [] a is a jagged array, an array of arrays where the sub arrays can for instance have different lengths. what is the difference between int [] [] and int [,]?

What is int *const in C++?

int *const. int *const is a constant pointer to integer. This means that the variable being declared is a constant pointer pointing to an integer. Effectively, this implies that the pointer shouldn’t point to some other address.

Can int be null by definition?

int cannot be null. int? is an alias for the Nullable<int> struct, which you can set to null. Note that int? is also a value type though... which contradicts the "by definition" of your first sentence... I still disagree with your first sentence, as int? is a value type with a null value.


4 Answers

int cannot be null.

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

like image 163
Kyle Trauberman Avatar answered Sep 18 '22 14:09

Kyle Trauberman


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;
like image 45
IROEGBU Avatar answered Sep 18 '22 14:09

IROEGBU


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.

like image 38
Anthony Pegram Avatar answered Sep 20 '22 14:09

Anthony Pegram


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.

like image 30
Matthew Steeples Avatar answered Sep 21 '22 14:09

Matthew Steeples