9

What is changed by applying nullable Operator on value type datatype that now it can store null.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94

5 Answers5

23

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
6

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
4

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

Frank Tzanabetis
  • 2,756
  • 2
  • 22
  • 22
2

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.

Michael Shimmins
  • 19,961
  • 7
  • 57
  • 90
-3

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?

Community
  • 1
  • 1
Coxy
  • 8,844
  • 4
  • 39
  • 62