1784

Ran across this line of code:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

What do the two question marks mean, is it some kind of ternary operator? It's hard to look up in Google.

Igor Kustov
  • 2,930
  • 2
  • 30
  • 29
Edward Tanguay
  • 176,854
  • 291
  • 683
  • 1,015
  • 56
    It's definitely *not* a ternary operator - it only has two operands! It's a bit like the conditional operator (which *is* ternary) but the null coalescing operator is a binary operator. – Jon Skeet Jan 15 '09 at 14:11
  • 96
    I explained it in an interview where the prospective employer had previously expressed doubts about my C# abilities, as I'd been using Java professionally for a while before. They hadn't heard of it before, and didn't question my familiarity with C# after that :) – Jon Skeet Jan 15 '09 at 16:32
  • 83
    @Jon Skeet There hasn't been such an epic fail to recognise skill since the guy who turned down the Beatles. :-) From now on just send them a copy of your book with a url link to your SO profile written on the inside cover. – Iain Holder Jan 16 '09 at 09:58
  • 6
    IainMH: For what it's worth, I hadn't *quite* started writing the book yet. (Or maybe I was just working on chapter 1 - something like that.) Admittedly a search for me would have quickly found my blog + articles etc. – Jon Skeet Jan 18 '09 at 16:45
  • 8
    Re: last sentence in the q - for future ref, SymbolHound is great for this kind of thing e.g. http://www.symbolhound.com/?q=%3F%3F&l=&e=&n=&u= [to anyone suspicious - I'm not affiliated in any way, just like a good tool when I find one...] – Steve Chambers Apr 25 '13 at 08:44
  • 5
    Searching for `C# ??` | `C# '??'` | `C# "??"` does not bring back expected results. Is the search engine testing if C# is null, and saying, No - Its actually `C#` - Here are your results for `C#` - Doh! – Piotr Kula Oct 31 '13 at 17:17
  • 4
    @ppumkin Just search for [`double question mark c#`](https://www.google.com/search?q=double%20question%20mark%20c%23&rct=j) in Google. – user692942 May 25 '16 at 11:03

19 Answers19

2349

It's the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator - MSDN.

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

expands to:

FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();

which further expands to:

if(formsAuth != null)
    FormsAuth = formsAuth;
else
    FormsAuth = new FormsAuthenticationWrapper();

In English, it means "If whatever is to the left is not null, use that, otherwise use what's to the right."

Note that you can use any number of these in sequence. The following statement will assign the first non-null Answer# to Answer (if all Answers are null then the Answer is null):

string Answer = Answer1 ?? Answer2 ?? Answer3 ?? Answer4;

Also it's worth mentioning while the expansion above is conceptually equivalent, the result of each expression is only evaluated once. This is important if for example an expression is a method call with side effects. (Credit to @Joey for pointing this out.)

Kolappan N
  • 2,178
  • 2
  • 26
  • 31
lc.
  • 105,606
  • 20
  • 147
  • 176
  • 20
    Potentially dangerous to chain these maybe – Coops Aug 04 '11 at 08:26
  • 74
    @CodeBlend, it's not dangerous. If you were to expand you'd just have a series of nested if/else statements. The syntax is just strange because you're not used to seeing it. – joelmdev Oct 26 '11 at 19:33
  • 1
    @jm2 As long as you review what you are doing for example if you meant to have seperate not nested ifs this would not be appropriate. (Reading this really showed me a decent(ish) example of how it can be used)[http://rlacovara.blogspot.com/2009/07/chaining-c-null-coalescing-operator.html] – Coops Oct 27 '11 at 08:09
  • @CodeBlend ..isn't that the case of "any" coding one does - to always review it :-) – SF Developer Aug 22 '12 at 18:08
  • It would be useful to add link to [MSDN documentation](http://msdn.microsoft.com/en-us/library/ms173224.aspx) into this answer - to keep everything together. :) Because, I've found it in the [next answer](http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c/446842#446842). – imy Sep 24 '12 at 11:41
  • it would also asign it to All the previous answear# variables that were null correct?? – Xitcod13 Oct 15 '12 at 08:03
  • 3
    @Xitcod13 No it wouldn't. Semantically, you would have to do `Answer1 ??= Answer2` to assign a value to Answer1, but `??=` isn't a valid operator. – lc. Oct 15 '12 at 08:29
  • @lc simply because it doesn't unmuddy the water. There is no indicated nesting - the null coalescing here is sequential. The concept is correct though. – Gusdor Sep 17 '13 at 07:15
  • 33
    It would have been a god-send if it worked for empty strings as well :) – Zyphrax Nov 19 '13 at 16:18
  • 6
    @Zyphrax, yes. Similar to how `||` works in JavaScript. – IsmailS Apr 21 '14 at 05:36
  • 5
    does the first argument get evaluated twice if it returns not-null first time? For example: x = f1() ?? f2(); would 'f1' be evaluated twice when it returns not-null first time? – Alex Sep 30 '14 at 14:31
  • 14
    @Gusdor `??` is left associative, so `a ?? b ?? c ?? d` is equivalent to `((a ?? b) ?? c ) ?? d`. "The assignment operators and the ternary operator (?:) are right associative. All other binary operators are left associative." Source: http://msdn.microsoft.com/en-us/library/ms173145.aspx – Mark E. Haase Dec 05 '14 at 20:45
  • 3
    Note that the expansion is not quite correct like shown here, as the language guarantees that the left operand is only evaluated once. This is not a problem in this particular case, but when you have a more complicated expression than a local variable on the left, it becomes important. – Joey Jul 24 '17 at 07:54
300

Just because no-one else has said the magic words yet: it's the null coalescing operator. It's defined in section 7.12 of the C# 3.0 language specification.

It's very handy, particularly because of the way it works when it's used multiple times in an expression. An expression of the form:

a ?? b ?? c ?? d

will give the result of expression a if it's non-null, otherwise try b, otherwise try c, otherwise try d. It short-circuits at every point.

Also, if the type of d is non-nullable, the type of the whole expression is non-nullable too.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • 3
    I like how you simplified its meaning to "null coalescing operator". Thats all I needed. – FrankO Sep 08 '15 at 15:25
  • 2
    Tangential but related: It's now in Swift, but it's very different: "Nil Coalescing Operator" https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html – Dan Rosenstark Oct 14 '15 at 19:21
73

It's the null coalescing operator.

http://msdn.microsoft.com/en-us/library/ms173224.aspx

Yes, nearly impossible to search for unless you know what it's called! :-)

EDIT: And this is a cool feature from another question. You can chain them.

Hidden Features of C#?

Community
  • 1
  • 1
Iain Holder
  • 13,731
  • 10
  • 61
  • 84
  • 38
    At least now, it's easy to search it, even if you don't know the name, I just googled *"double question mark c#"* – stivlo Dec 21 '11 at 08:18
31

Thanks everybody, here is the most succinct explanation I found on the MSDN site:

// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;
Edward Tanguay
  • 176,854
  • 291
  • 683
  • 1,015
  • 4
    This hints at an important aspect of the ?? operator -- it was introduced to assist in working with nullable types. In your example, "x" is of type "int?" (Nullable). – Drew Noakes Jun 05 '09 at 12:56
  • 9
    @vitule no, if the second operand of the null coalescing operator is non-nullable, then the result is non-nullable (and `-1` is just a plain `int`, which is non-nullable). – Suzanne Soy Jul 11 '13 at 09:52
25

enter image description here

The two question marks (??) indicate that its a Coalescing operator.

Coalescing operator returns the first NON-NULL value from a chain. You can see this youtube video which demonstrates the whole thing practically.

But let me add more to what the video says.

If you see the English meaning of coalescing it says “consolidate together”. For example below is a simple coalescing code which chains four strings.

So if str1 is null it will try str2, if str2 is null it will try str3 and so on until it finds a string with a non-null value.

string final = str1 ?? str2 ?? str3 ?? str4;

In simple words Coalescing operator returns the first NON-NULL value from a chain.

Neuron
  • 3,776
  • 3
  • 24
  • 44
Shivprasad Koirala
  • 23,328
  • 7
  • 76
  • 65
  • 1
    I like this explanation because it has a diagram, and then the last sentence explains it in such simple terms! It makes it easy to comprehend and boosts my confidence in use. Thanks! – Joshua K Aug 02 '18 at 14:13
  • when we take just the English meaning of coalesce, we could expect the result to be some kind of union, or merge, of the arguments. It certainly is not the best metaphor for what it actually does. But there is a long standing predecessor, the t-sql `COALESCE` function, which does the same, and - what few developers might be aware of - has a variable argument list so it can take more than two candidate values for coalescing, just like the .NET ?? operator. – Cee McSharpface Nov 18 '20 at 09:43
22

?? is there to provide a value for a nullable type when the value is null. So, if formsAuth is null, it will return new FormsAuthenticationWrapper().

Vishal Suthar
  • 16,137
  • 2
  • 50
  • 97
D'Arcy Rittich
  • 153,827
  • 35
  • 271
  • 277
14

It's short hand for the ternary operator.

FormsAuth = (formsAuth != null) ? formsAuth : new FormsAuthenticationWrapper();

Or for those who don't do ternary:

if (formsAuth != null)
{
  FormsAuth = formsAuth;
}
else
{
  FormsAuth = new FormsAuthenticationWrapper();
}
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
Benjamin Autin
  • 4,015
  • 23
  • 33
  • 3
    I've only corrected the spelling of "ternary" but really the operator you mean is the conditional operator. It happens to be the only ternary operator in C#, but at some point they might add another one, at which point "ternary" will be ambiguous but "conditional" won't. – Jon Skeet Jan 15 '09 at 14:10
  • It's shorthand for something you can *do* with the ternary (conditional) operator. In your long form, both the test (`!= null`) and the second `formsAuth` (after the `?`) could be altered; in the null coalesce form, both implicitly take the values you have supplied. – Bob Sammers Apr 20 '16 at 12:24
12

If you're familiar with Ruby, its ||= seems akin to C#'s ?? to me. Here's some Ruby:

irb(main):001:0> str1 = nil
=> nil
irb(main):002:0> str1 ||= "new value"
=> "new value"
irb(main):003:0> str2 = "old value"
=> "old value"
irb(main):004:0> str2 ||= "another new value"
=> "old value"
irb(main):005:0> str1
=> "new value"
irb(main):006:0> str2
=> "old value"

And in C#:

string str1 = null;
str1 = str1 ?? "new value";
string str2 = "old value";
str2 = str2 ?? "another new value";
Sarah Vessels
  • 27,994
  • 29
  • 147
  • 217
  • 4
    `x ||= y` desugars to something like `x = x || y`, so `??` is actually more similar to plain `||` in Ruby. – qerub Feb 02 '13 at 00:03
  • 7
    Note that `??` only cares about `null`, whereas the `||` operator in Ruby, as in most languages, is more about `null`, `false`, or anything that can be considered a boolean with a value of `false` (e.g. in some languages, `""`). This is not a good or bad thing, merely a difference. – Tim S. May 08 '13 at 20:15
11

Nothing dangerous about this. In fact, it is beautiful. You can add default value if that is desirable, for example:

CODE

int x = x1 ?? x2 ?? x3 ?? x4 ?? 0;
Agustin Meriles
  • 4,672
  • 3
  • 27
  • 43
dqninh
  • 111
  • 1
  • 2
  • 1
    So x1, x2, x3, and x4 could be Nullable types, example: `int? x1 = null;` Is that right – Kevin Meredith Jun 25 '13 at 19:02
  • 1
    @KevinMeredith `x1` - `x4` MUST be nullable types: it makes no sense to say, effectively, "the result is `0` if `x4` is a value which it can't possibly take" (`null`). "Nullable type" here includes both nullable *value* types and reference types, of course. It is a compile-time error if one or more of the chained variables (except the last one) isn't nullable. – Bob Sammers Apr 20 '16 at 12:13
11

As correctly pointed in numerous answers that is the "null coalescing operator" (??), speaking of which you might also want to check out its cousin the "Null-conditional Operator" (?. or ?[) that is an operator that many times it is used in conjunction with ??

Null-conditional Operator

Used to test for null before performing a member access (?.) or index (?[) operation. These operators help you write less code to handle null checks, especially for descending into data structures.

For example:

// if 'customers' or 'Order' property or 'Price' property  is null,
// dollarAmount will be 0 
// otherwise dollarAmount will be equal to 'customers.Order.Price'

int dollarAmount = customers?.Order?.Price ?? 0; 

the old way without ?. and ?? of doing this is

int dollarAmount = customers != null 
                   && customers.Order!=null
                   && customers.Order.Price!=null 
                    ? customers.Order.Price : 0; 

which is more verbose and cumbersome.

brakeroo
  • 1,104
  • 9
  • 21
11

coalescing operator

it's equivalent to

FormsAuth = formsAUth == null ? new FormsAuthenticationWrapper() : formsAuth
Vishal Suthar
  • 16,137
  • 2
  • 50
  • 97
aku
  • 115,356
  • 31
  • 164
  • 200
10

Note:

I have read whole this thread and many others but I can't find as thorough answer as this is.

By which I completely understood the "why to use ?? and when to use ?? and how to use ??."

Source:

Windows communication foundation unleashed By Craig McMurtry ISBN 0-672-32948-4

Nullable Value Types

There are two common circumstances in which one would like to know whether a value has been assigned to an instance of a value type. The first is when the instance represents a value in a database. In such a case, one would like to be able to examine the instance to ascertain whether a value is indeed present in the database. The other circumstance, which is more pertinent to the subject matter of this book, is when the instance represents a data item received from some remote source. Again, one would like to determine from the instance whether a value for that data item was received.

The .NET Framework 2.0 incorporates a generic type definition that provides for cases like these in which one wants to assign null to an instance of a value type, and test whether the value of the instance is null. That generic type definition is System.Nullable<T>, which constrains the generic type arguments that may be substituted for T to value types. Instances of types constructed from System.Nullable<T> can be assigned a value of null; indeed, their values are null by default. Thus, types constructed from System.Nullable<T> may be referred to as nullable value types. System.Nullable<T> has a property, Value, by which the value assigned to an instance of a type constructed from it can be obtained if the value of the instance is not null. Therefore, one can write:

System.Nullable<int> myNullableInteger = null;
myNullableInteger = 1;
if (myNullableInteger != null)
{
Console.WriteLine(myNullableInteger.Value);
}

The C# programming language provides an abbreviated syntax for declaring types constructed from System.Nullable<T>. That syntax allows one to abbreviate:

System.Nullable<int> myNullableInteger;

to

int? myNullableInteger;

The compiler will prevent one from attempting to assign the value of a nullable value type to an ordinary value type in this way:

int? myNullableInteger = null;
int myInteger = myNullableInteger;

It prevents one from doing so because the nullable value type could have the value null, which it actually would have in this case, and that value cannot be assigned to an ordinary value type. Although the compiler would permit this code,

int? myNullableInteger = null;
int myInteger = myNullableInteger.Value;

The second statement would cause an exception to be thrown because any attempt to access the System.Nullable<T>.Value property is an invalid operation if the type constructed from System.Nullable<T> has not been assigned a valid value of T, which has not happened in this case.

Conclusion:

One proper way to assign the value of a nullable value type to an ordinary value type is to use the System.Nullable<T>.HasValue property to ascertain whether a valid value of T has been assigned to the nullable value type:

int? myNullableInteger = null;
if (myNullableInteger.HasValue)
{
int myInteger = myNullableInteger.Value;
}

Another option is to use this syntax:

int? myNullableInteger = null;
int myInteger = myNullableInteger ?? -1;

By which the ordinary integer myInteger is assigned the value of the nullable integer "myNullableInteger" if the latter has been assigned a valid integer value; otherwise, myInteger is assigned the value of -1.

Pang
  • 8,605
  • 144
  • 77
  • 113
Muhammad Waqas Dilawar
  • 1,185
  • 1
  • 18
  • 25
10

For your amusement only (knowing you are all C# guys ;-).

I think it originated in Smalltalk, where it has been around for many years. It is defined there as:

in Object:

? anArgument
    ^ self

in UndefinedObject (aka nil's class):

? anArgument
    ^ anArgument

There are both evaluating (?) and non-evaluating versions (??) of this.
It is often found in getter-methods for lazy-initialized private (instance) variables, which are left nil until really needed.

blabla999
  • 3,074
  • 19
  • 22
  • sounds like wrapping ViewState with a property on a UserControl. Initialize only on the first get, if it´s not set before. =) – Seiti Jan 15 '09 at 14:52
9

Some of the examples here of getting values using coalescing are inefficient.

What you really want is:

return _formsAuthWrapper = _formsAuthWrapper ?? new FormsAuthenticationWrapper();

or

return _formsAuthWrapper ?? (_formsAuthWrapper = new FormsAuthenticationWrapper());

This prevents the object from being recreated every time. Instead of the private variable remaining null and a new object getting created on every request, this ensures the private variable is assigned if the new object is created.

KingOfHypocrites
  • 8,717
  • 7
  • 44
  • 66
3

Others have described the Null Coalescing Operator quite well. In cases where a single test for null is required, the shortened syntax ??= can add readability.

Legacy null test:

if (myvariable == null)
{
    myvariable = new MyConstructor();
}

Using the Null Coalescing Operator this can be written:

myvariable = myvariable ?? new MyConstructor();

which can also be written with the shortened syntax:

myvariable ??= new MyConstructor();

Some find it more readable and succinct.

Thane Plummer
  • 4,367
  • 2
  • 23
  • 27
1

It's a null coalescing operator that works similarly to a ternary operator.

    a ?? b  => a !=null ? a : b 

Another interesting point for this is, "A nullable type can contain a value, or it can be undefined". So if you try to assign a nullable value type to a non-nullable value type you will get a compile-time error.

int? x = null; // x is nullable value type
int z = 0; // z is non-nullable value type
z = x; // compile error will be there.

So to do that using ?? operator:

z = x ?? 1; // with ?? operator there are no issues
Community
  • 1
  • 1
Ashish Mishra
  • 558
  • 6
  • 14
1
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

is equivalent to

FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();

But the cool thing about it is you can chain them, like other people said. The one thin not touched upon is that you can actually use it to throw an exception.

A = A ?? B ?? throw new Exception("A and B are both NULL");
NolePTR
  • 122
  • 5
  • It's really great that you included examples in your post, although the question was looking for an explanation of what the operator is or does. – TGP1994 Mar 31 '18 at 21:53
1

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

int? variable1 = null;
int variable2  = variable1 ?? 100;

Set variable2 to the value of variable1, if variable1 is NOT null; otherwise, if variable1 == null, set variable2 to 100.

Adeel
  • 2,657
  • 7
  • 21
  • 31
-1

In simplest way, two question marks are called "Coalescing Operator", which returns first non null value from the chain.

e.g if you are getting a values from a nullable object, in a variable which is not nullable, then you can use this operator. i.e.

int a = 1;
int? b = null;
a = b??0;

Result of above equation would be zero, because b is null and we have used the ?? operator along with zero, which means that it will return 0 if and only if b is null.

int a = 1;
int? b = 15;
a = b??0;

In above equation, a will get value "15" because b has a valid value and is not null. Also, you can not use ?? operator on a non nullable object.

In above examples, I used ?? 0, however a complete new equation can also be used after ?? operator.

Such as
a = b ?? ( x==1 ? 10 : 15)
I hope it will clear your question.