Why can't int be null? How does nullable int (int?) Work in C #?

I am new to C # and just found out that objects can be empty in C #, but int cannot.

Also how does the null ( int? ) Function work in C #?

+7
source share
6 answers

int is a primitive type, and only ReferenceType (objects) are NULL. You can make int a null value by wrapping it in an object:

 System.Nullable<int> i; 

-or -

 int? i; 

http://msdn.microsoft.com/en-us/library/2cf62fcy(v=vs.80).aspx

+11
source

Value types, such as int , contain direct values, not references like ReferenceType . The value cannot be null, but the reference can be null, which means that it does not indicate anything. Value cannot have anything in it. The value always contains some value

Nullable or Nullable types are special value types that, in addition to the normal values โ€‹โ€‹of value types, also support an additional null value, like reference types

+2
source

int is the type of value. This is really an Int32 type. Objects that can be null are reference types. At the basic level, the difference is that the type of the value will store the value with a variable, where the reference type will store the reference to the value. The reference may be null, which means that it does not indicate a value. If the value type always matters.

Nullable wraps a value type that allows you to be null. Using int? it's just a compiler to declare a type with a null value.

+2
source

Can you have a nullable int with int? or Nullable<int> are both exactly the same.

+2
source

Objects are reference types, and they cannot refer to anything or NULL, but int is a value type that can contain only a value and cannot refer to anything.

+1
source

The primitive int type cannot express null in its binary representation, but the Nullable<int> added an extra byte to express the null information of this value.

0
source

All Articles