What does a double question mark do in C#?

C#Null Coalescing-OperatorNull Coalescing

C# Problem Overview


> 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;

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

From http://en.wikipedia.org/wiki/C_Sharp_syntax">Wikipedia</a>;:

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

x = (y != null ? y : z);

Solution 3 - C#

Use y if not null, otherwise use z.

Solution 4 - C#

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"

Solution 5 - C#

If y is null x will be set to z.

Solution 6 - C#

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

Solution 7 - C#

.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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontom dView Question on Stackoverflow
Solution 1 - C#Alexis AbrilView Answer on Stackoverflow
Solution 2 - C#Austin SalonenView Answer on Stackoverflow
Solution 3 - C#CodeSpeakerView Answer on Stackoverflow
Solution 4 - C#Michael EdwardsView Answer on Stackoverflow
Solution 5 - C#Spencer RuportView Answer on Stackoverflow
Solution 6 - C#itsmattView Answer on Stackoverflow
Solution 7 - C#123DeveloperView Answer on Stackoverflow