34

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?

Community
  • 1
  • 1
Ante
  • 8,157
  • 17
  • 56
  • 69
  • 4
    For lots of other fun syntax in C# check this out: http://stackoverflow.com/questions/9033/hidden-features-of-c – tster Nov 19 '09 at 08:14

12 Answers12

52

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.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
35

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;
BFree
  • 97,931
  • 20
  • 150
  • 197
  • Actually x ?? y calls x.GetValueOrDefault(y) (the IL code: `call instance !0 valuetype [mscorlib]System.Nullable`1::GetValueOrDefault()`) – Aidiakapi Sep 08 '11 at 11:35
12

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

JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
8

It is a shortcut for:

Text = (category == null ? "Home" : category);
Konamiman
  • 47,560
  • 16
  • 107
  • 133
8

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

Scott Ferguson
  • 7,290
  • 7
  • 34
  • 64
7

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
Daniel Magliola
  • 27,613
  • 56
  • 154
  • 235
Darren Kopp
  • 71,547
  • 9
  • 71
  • 90
5

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

tanascius
  • 50,043
  • 21
  • 106
  • 130
4

if category is null, Text will become "Home"

brysseldorf
  • 211
  • 2
  • 7
4

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.

Chris
  • 26,212
  • 23
  • 116
  • 214
  • SRP: http://en.wikipedia.org/wiki/Single_responsibility_principle – Chris May 06 '09 at 00:33
  • I wonder if the compiler renders it differently from an IF stament in the MSIL. Anyone know? – Jagd May 06 '09 at 00:48
  • 1
    doubt it, but i won't lose sleep over not knowing. – Darren Kopp May 06 '09 at 01:02
  • 2
    I don't know that "if" statements are so ugly, but "assign only once" is a nice paradigm when you can do it without destroying the natural flow of the code. – Roboprog May 06 '09 at 01:08
  • Whether "if" statements are ugly is surely subjective, and I doubt my way improves performance. As I said, it just makes the code cleaner and easier to follow, IMO. – Chris May 06 '09 at 03:22
2

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

2

Returns the first not-null value. Handy.

marcc
  • 12,129
  • 7
  • 47
  • 59
1

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

JP Alioto
  • 43,483
  • 5
  • 85
  • 112