Champion issue: dotnet#8631
Support an expression of the form await? e, which awaits e if it is non-null, otherwise it results in null.
This is a common coding pattern, and this feature would have nice synergy with the existing null-propagating and null-coalescing operators.
We add a new form of the await_expression:
await_expression
: 'await' '?' unary_expression
;The null-conditional await operator awaits its operand only if that operand is non-null. Otherwise the result of applying the operator is null.
The type of the result is computed using the rules for the null-conditional operator §11.7.7.
NOTE: If
eis of typeTask, thenawait? e;would do nothing ifeisnull, and awaiteif it is notnull.If
eis of typeTask<K>whereKis a value type, thenawait? ewould yield a value of typeK?.
As with any language feature, we must question whether the additional complexity to the language is repaid in the additional clarity offered to the body of C# programs that would benefit from the feature.
Although it requires some boilerplate code, uses of this operator can often be replaced by an expression something like (e == null) ? null : await e or a statement like if (e != null) await e.
- Requires LDM review
None.