Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

? (nullable) operator in C# [duplicate]

Tags:

c#

nullable

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

like image 723
Govind Malviya Avatar asked Jul 06 '10 05:07

Govind Malviya


People also ask

What is null and nullable?

Technically a null value is a reference (called a pointer in some languages) to an empty area of memory. Reference variables (variables that contain an address for data rather than the data itself) are always nullable , which means they are automatically capable of having a null value.

What do you mean by null operator?

A null coalescing operator, in C#, is an operator that is used to check whether the value of a variable is null. It is represented by the symbol "??". The null coalescing operator allows for the selection of the first non-null value from a pair of values.

What is nullable types and null coalescing operator in C#?

Nullable types work as a connector between a database and C# code to provide a way to transform the nulls to be used in C# code. Null Coalescing operators simplify the way to check for nulls and shorten the C# code when the developer is dealing with nulls.

How do you use nullable?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.


2 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.

like image 158
Jon Skeet Avatar answered Sep 21 '22 00:09

Jon Skeet


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.

like image 33
Oded Avatar answered Sep 19 '22 00:09

Oded