What does a double question mark do in C#? [duplicate]
Asked Answered
F

7

91

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;
Forwhy answered 22/10, 2009 at 15:49 Comment(4)
this has been asked a few times: https://mcmap.net/q/45017/-what-do-two-question-marks-together-mean-in-c #447335Susurrous
stackoverflow.com/questions/827454Exigent
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...]Depolarize
Ya, I thought i was gonna make big coin, but I had a brainfart and couldn't remember the dang jargon in time. Baw.Leopold
S
105

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.

Spermiogenesis answered 22/10, 2009 at 15:51 Comment(0)
S
33

From Wikipedia:

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

x = (y != null ? y : z);
Screwdriver answered 22/10, 2009 at 15:53 Comment(0)
E
27

Use y if not null, otherwise use z.

Exo answered 22/10, 2009 at 15:51 Comment(0)
A
13

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"

Aeromedical answered 22/10, 2009 at 15:52 Comment(0)
W
9

If y is null x will be set to z.

Wedding answered 22/10, 2009 at 15:51 Comment(0)
L
1

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

MSDN information on this:

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

Libel answered 22/10, 2009 at 15:53 Comment(0)
S
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

Scorn answered 22/10, 2009 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.