Question

This question already has an answer here:

I was wondering about ?? signs in C# code. What is it for? And how can I use it?

What about int?? Is it a nullable int?

See also:

?? Null Coalescing Operator —> What does coalescing mean?

Was it helpful?

Solution

It's called the "null coalescing operator" and works something like this:

Instead of doing:

int? number = null;
int result = number == null ? 0 : number;

You can now just do:

int result = number ?? 0;

OTHER TIPS

It's the null coalescing operator. It was introduced in C# 2.

The result of the expression a ?? b is a if that's not null, or b otherwise. b isn't evaluated unless it's needed.

Two nice things:

  • The overall type of the expression is that of the second operand, which is important when you're using nullable value types:

    int? maybe = ...;
    int definitely = maybe ?? 10;
    

    (Note that you can't use a non-nullable value type as the first operand - it would be pointless.)

  • The associativity rules mean you can chain this really easily. For example:

    string address = shippingAddress ?? billingAddress ?? contactAddress;
    

That will use the first non-null value out of the shipping, billing or contact address.

That is the coalesce operator. It essentially is shorthand for the following

x ?? new Student();
x != null ? x : new Student();

MSDN Documentation on the operator

It's the new Null Coalesce operator.

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

You can read about it here: http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

It is a shortcut for:

Text = (category == null ? "Home" : category);

it's the coalesce operator. it will return another value if the first value is null

string value1 = null;
string value2 = "other";

string value3 = value1 ?? value2; // assigns "other" to value 3

it checks if category is null - when this is the case the null value is replaced by "Home".

One of my favorite uses for the null coalescing operator is to avoid if statements in my code (I think if statements are ugly and just clutter things up most times). For example, take a typical scenario where one might choose to load something from cache if available, otherwise load from the db and populate the cache.

private SomeData GetData() {
    var data = HttpRuntime.Cache.Get("key") as SomeData;

    if (data == null) {
        data = DAL.GetData(some parameters...);
        HttpRuntime.Cache.Add("key", data, ....);
    }

    return data;
}

To me, that's ugly code. I may be a bit anal, but why not refactor it to this instead?

private SomeData GetDataAndCache() {
    var data = DAL.GetData(some parameters...);
    HttpRuntime.Cache.Add("key", data, ....);
    return data;
}

private SomeData GetData() {
    var data = HttpRuntime.Cache.Get("key") as SomeData;
    return data ?? GetDataAndCache();
}

It more closely follows SRP and is cleaner and easier to read, IMO. The functions perform exactly one clearly identifiable function each.

if category is null, Text will become "Home"

Returns the first not-null value. Handy.

?? Null-Coalescing Operator

int? is a nullable int, which means it can have the values of a normal int and null. Read this for details.

That's the null-coalescing operator . It's used with nullable types (among other things, sorry :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top