What Does This Two Question Mark Mean

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 are the ?? double question marks in Dart?

The ?? double question mark operator means "if null". Take the following expression, for example.

String a = b ?? 'hello';

This means a equals b, but if b is null then a equals 'hello'.

Another related operator is ??=. For example:

b ??= 'hello';

This means if b is null then set it equal to hello. Otherwise, don't change it.

Reference

  • A Tour of the Dart Language: Operators
  • Null-aware operators in Dart

Terms

The Dart 1.12 release news collectively referred to the following as null-aware operators:

  • ?? -- if null operator
  • ??= -- null-aware assignment
  • x?.p -- null-aware access
  • x?.m() -- null-aware method invocation

What does double question mark (??) operator mean in PHP

It's the "null coalescing operator", added in php 7.0. The definition of how it works is:

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

So it's actually just isset() in a handy operator.

Those two are equivalent1:

$foo = $bar ?? 'something';
$foo = isset($bar) ? $bar : 'something';

Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

And original RFC https://wiki.php.net/rfc/isset_ternary


EDIT: As this answer gets a lot of views, little clarification:

1There is a difference: In case of ??, the first expression is evaluated only once, as opposed to ? :, where the expression is first evaluated in the condition section, then the second time in the "answer" section.

What does this two question mark mean?

It's the coalescing operator. It returns the first expression (featureImageSizeOptional) if it's non-nil. If the first expression is nil, the operator returns the second expression (CGSizeZero).

See the Language Guide for more info.

What Do Two Question Marks in a Row in a Ternary Clause Mean?

It is simply a nested ternary statement. Clearer by adding parentheses:

number == null ? (required ? 1 : 2) : 3;

what would the inputs need to be for 2 to be returned?

number = null and required = false

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://learn.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.



Related Topics



Leave a reply



Submit