views:

648

answers:

12

I was wondering about "??" signs in c# code.. what is it for? And how can i use it?

what about "int?"? is it nullable int?

See also:

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

+12  A: 

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
A -1 for what exactly?
JaredPar
+5  A: 

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
Darren Kopp
+6  A: 

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
+2  A: 

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

TobiasBohnen
+18  A: 

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
A: 

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

JP Alioto
+1  A: 

Returns the first not-null value. Handy.

marcc
+3  A: 

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
Thanks. And SRP is..?
Ante B.
SRP: http://en.wikipedia.org/wiki/Single_responsibility_principle
Chris
I wonder if the compiler renders it differently from an IF stament in the MSIL. Anyone know?
Jagd
doubt it, but i won't lose sleep over not knowing.
Darren Kopp
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
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
+3  A: 

if category is null, Text will become "Home"

brysseldorf
+4  A: 

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

tanascius
+7  A: 

It is a shortcut for:

Text = (category == null ? "Home" : category);
Konamiman
+29  A: 

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 for "The second operand isn't evaluated unless it's needed". This sort of things are easy to miss.
Konamiman
type: `int definitely = maybe ? 10;` should be `int definitely = maybe ?? 10;`
tster
@tster: Fixed, thanks.
Jon Skeet
This should be merged with http://stackoverflow.com/questions/827454/what-is-the-operator-for its a more complete answer
Sam Saffron
it's equivalent to a!=null?a:b
dharga
@dharga: Except that it only evaluates `a` once.
Jon Skeet