What Do Two Question Marks Together Mean in C#

What do two question marks together mean in C#?

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

What does a double question mark do in 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.

What is the double question mark equals sign (??=) in C#?

It's the same as this:

if (a == null) {
a = 10;
}

It's an operator introduced in C# 8.0: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator

Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.

Double question marks ('??') vs if when assigning same var

Don't worry about the performance, it will be negligible.

If you are curious about it, write some code to test the performance using Stopwatch and see. I suspect you'll need to do a few million iterations to start seeing differences though.

You can also never assume about the implementation of things, they are liable to change in future - invalidating your assumptions.

My assumption is the performance difference is likely very, very small. I'd go for the null coalescing operator for readability personally, it is nice and condense and conveys the point well enough. I sometimes do it for lazy-load checking:

_lazyItem = _lazyItem ?? new LazyItem();

What does question mark and dot operator ?. mean in C# 6.0?

It's the null conditional operator. It basically means:

"Evaluate the first operand; if that's null, stop, with a result of null. Otherwise, evaluate the second operand (as a member access of the first operand)."

In your example, the point is that if a is null, then a?.PropertyOfA will evaluate to null rather than throwing an exception - it will then compare that null reference with foo (using string's == overload), find they're not equal and execution will go into the body of the if statement.

In other words, it's like this:

string bar = (a == null ? null : a.PropertyOfA);
if (bar != foo)
{
...
}

... except that a is only evaluated once.

Note that this can change the type of the expression, too. For example, consider FileInfo.Length. That's a property of type long, but if you use it with the null conditional operator, you end up with an expression of type long?:

FileInfo fi = ...; // fi could be null
long? length = fi?.Length; // If fi is null, length will be null


Related Topics



Leave a reply



Submit