What is changed by applying nullable Operator on value type datatype that now it can store null.
5 Answers
As others have said, "?" is just shorthand for changing it to Nullable<T>
. This is just another value type with a Boolean flag to say whether or not there's really a useful value, or whether it's the null value for the type. In other words, Nullable<T>
looks a bit like this:
public struct Nullable<T>
{
private readonly bool hasValue;
public bool HasValue { get { return hasValue; } }
private readonly T value;
public T value
{
get
{
if (!hasValue)
{
throw new InvalidOperationException();
}
return value;
}
}
public Nullable(T value)
{
this.value = value;
this.hasValue = true;
}
// Calling new Nullable<int>() or whatever will use the
// implicit initialization which leaves value as default(T)
// and hasValue as false.
}
Obviously in the real code there are more methods (like GetValueOrDefault()
) and conversion operators etc. The C# compiler adds lifted operators which effectively proxy to the original operators for T
.
At the risk of sounding like a broken record, this is still a value type. It doesn't involve boxing... and when you write:
int? x = null;
that's not a null reference - it's the null value of Nullable<int>
, i.e. the one where hasValue
is false
.
When a nullable type is boxed, the CLR has a feature whereby the value either gets boxed to a null reference, or a plain boxed T. So if you have code like this:
int? x = 5;
int y = 5;
object o1 = x;
object o2 = y;
The boxed values referred to by o1
and o2
are indistinguishable. You can't tell that one is the result of boxing a nullable type.

- 1,421,763
- 867
- 9,128
- 9,194
The ?
syntax is syntactic sugar to the Nullable<T>
struct.
In essence, when you write int? myNullableInt
, the compiler changes it to Nullable<int> myNullableInt
.
From MSDN (scroll down to "Nullable Types Overview"):
The syntax T? is shorthand for Nullable, where T is a value type. The two forms are interchangeable.

- 489,969
- 99
- 883
- 1,009
-
2+1 for the sugar, but I don't think the word "operator" is correct here. – Kobi Jul 06 '10 at 06:04
-
@Kobi - fair enough. Answer updated. – Oded Jul 06 '10 at 06:07
-
I would argue that it is just syntactic sugar, see this [answer](http://stackoverflow.com/a/24738380/1552016). – qqbenq Jul 14 '14 at 14:13
Nothing is changed on the value type itself, it's simply wrapped in a System.Nullable<T>
struct.
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

- 2,756
- 2
- 22
- 22
The type changes from what it used to be to a Nullable type.
If you had an int, and decided to make it an int?:
int myInt;
And you make it:
int? myInt;
it is now:
Nullable<int> myInt;
In reality.

- 19,961
- 7
- 57
- 90
In basic terms, a nullable type is a boxed version of the normal type that has an extra boolean field called hasValue on it. When this field is set to false, then the instance is null.
Check out this answer for a bit more detail on the CLR implementation, and why they might have chosen it: Boxing / Unboxing Nullable Types - Why this implementation?
-
-
2I wouldn't call it a boxed version. Boxing creates a reference type representation of a value type. Nullable
is a struct. – Brian Rasmussen Jul 06 '10 at 06:14