18 out of 24 rated this helpful - Rate this topic

?? Operator (C# Reference)

The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

For more information, see Nullable Types (C# Programming Guide).

The result of a ?? operator is not considered to be a constant even if both its arguments are constants.


class NullCoalesce
{
    static int? GetNullableInt()
    {
        return null;
    }

    static string GetStringValue()
    {
        return null;
    }

    static void Main()
    {
        // ?? operator example.
        int? x = null;

        // y = x, unless x is null, in which case y = -1.
        int y = x ?? -1;

        // Assign i to return value of method, unless
        // return value is null, in which case assign
        // default value of int to i.
        int i = GetNullableInt() ?? default(int);

        string s = GetStringValue();
        // ?? also works with reference types. 
        // Display contents of s, unless s is null, 
        // in which case display "Unspecified".
        Console.WriteLine(s ?? "Unspecified");
    }
}


Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Associativity
Note that the ?? operator is RIGHT associative, despite the C# 4.0 spec and MSDN stating that all binary operators other than assignments and the conditional (ternary) operator are left associative (that was once true, but no longer once ?? was added). 


Edit by SJ at MSFT: This will be updated in the language spec for the next release. Thanks for pointing it out.
Why returning non constant?
"The result of a ?? operator is not considered to be a constant even if both its arguments are constants."

1. Why is that? Is this a design choice, or is there any other reason preventing the result from being a constant?
2. What follows from this? When should someone care?
The operand can be used only when Implicit convertion between both side of the ?? is possible
E.g If we have the following classes
class A{}
class B:A{}
class C:A{}

B b = new B();
C c = null;
//The folowing is not allowed:
A a = c ?? b;
And if both operands are null?
What is the result of the operation if both operands are null? Null? Exception thrown?

-----

It only checks the left side, and if it is null, then the right side is returned unconditionally.

My observation is that it returns null if both are null. I tried following:

DateTime? x = null; // DateTime.Now;
DateTime? y = null; // DateTime.Now.AddDays(1);

object z = x ?? y;

Console.WriteLine("Z = " + z);

z was null on x, y as null. $0 $0Edit:$0 $0In the above example 'z' is nullable. If we have "DateTime z" then a compile error will be generated because it is guaranteed to be null.