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
When to Use Static Classes in C#
Is There a Difference Between "Throw" and "Throw Ex"
Input String Was Not in a Correct Format
JavaScriptserializer - Json Serialization of Enum as String
How to Call Stored Procedure in Entity Framework 6 (Code-First)
Json.Net: How to Deserialize Without Using the Default Constructor
C# Datetime to "Yyyymmddhhmmss" Format
How to Get the Type of T from a Member of a Generic Class or Method
Create a Strongly Typed C# Object from Json Object With Id as the Name
How to Read a Text File Reversely With Iterator in C#
What Are the Default Access Modifiers in C#
Find and Extract a Number from a String
No Connection Could Be Made Because the Target Machine Actively Refused It
Using New Unity Videoplayer and Videoclip API to Play Video
How to Dynamically Generate HTML Code Using .Net'S Webbrowser or Mshtml.Htmldocument