You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the documentation, the null-forgiving operator has no effect at run time. However, it has a surprising side effect of breaking a null_conditional_operations chain at compile time, which may change evaluation of the expression. Consider this example:
int[]a=null;// s1 is null: ToString() is not called at allstrings1=a?.First().ToString();// s2 is the empty string: ToString() is called on default(int?)strings2=a?.First()!.ToString();
Adding the ! operator effectively changes parsing of the expression to (a?.First())!.ToString(). Since this behavior is surprising for many developers (e.g., dotnet/docs#17988), I am wondering whether not allowing ! in null_conditional_operations was due to omission rather than intentional design. In the former case we might consider fixing this, in the latter — I would like to learn arguments that led to this decision.
The text was updated successfully, but these errors were encountered:
According to the null-conditional and null-forgiving operator specifications, a?.b! is not even a legal expression, because ! may follow only a primary_expression and not a null_conditional_expression:
According to the documentation, the null-forgiving operator has no effect at run time. However, it has a surprising side effect of breaking a
null_conditional_operations
chain at compile time, which may change evaluation of the expression. Consider this example:Adding the
!
operator effectively changes parsing of the expression to(a?.First())!.ToString()
. Since this behavior is surprising for many developers (e.g., dotnet/docs#17988), I am wondering whether not allowing!
innull_conditional_operations
was due to omission rather than intentional design. In the former case we might consider fixing this, in the latter — I would like to learn arguments that led to this decision.The text was updated successfully, but these errors were encountered: