80

Possible Duplicates:
?? Null Coalescing Operator --> What does coalescing mean?
What do two question marks together mean in C#?

I couldn't find this question being asked here so I figured I would ask it. What does a double question mark do in C#?

Example:

x = y ?? z;
Dharman
  • 26,923
  • 21
  • 73
  • 125
tom d
  • 943
  • 1
  • 7
  • 10
  • this has been asked a few times: http://stackoverflow.com/questions/1064074/operator-in-c/ http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c – Kirschstein Oct 22 '09 at 15:54
  • 1
    http://stackoverflow.com/questions/827454/ – John Gietzen Oct 22 '09 at 15:54
  • It gets you, and everyone who answers before the topic is closed, a lot of rep :) [It always amazes me how fast null coalescing questions and answer get rep here...] – Reed Copsey Oct 22 '09 at 15:55
  • Ya, I thought i was gonna make big coin, but I had a brainfart and couldn't remember the dang jargon in time. Baw. –  Oct 22 '09 at 16:00

7 Answers7

93

This is a null coalescing operator. The method above states x is assigned y's value, unless y is null, in which case it is assigned z's value.

Alexis Abril
  • 6,309
  • 4
  • 30
  • 52
26

From Wikipedia:

It's the null-coalesce operator and shorthand for this:

x = (y != null ? y : z);
Austin Salonen
  • 47,582
  • 15
  • 104
  • 136
19

Use y if not null, otherwise use z.

Jeankowkow
  • 784
  • 11
  • 30
CodeSpeaker
  • 760
  • 8
  • 22
10

If a the value y is null then the value z is assigned.

For example:

x = Person.Name ?? "No Name";

If name is null x will have the value "No Name"

Michael Edwards
  • 5,928
  • 6
  • 39
  • 73
8

If y is null x will be set to z.

Spencer Ruport
  • 34,575
  • 11
  • 83
  • 145
1

As others have stated, it is the null coalescing operator.

MSDN information on this:

https://docs.microsoft.com/dotnet/csharp/language-reference/operators/null-coalescing-operator

itsmatt
  • 30,859
  • 10
  • 99
  • 162
1

.Net framework 2.0 onwards allow null values to Nullable value types.

here in this case, it says x equals y if it has some value (ie not null) or else equals z

123Developer
  • 1,453
  • 3
  • 16
  • 24