-
Consider the following snippet: object?[] a = ...
object[] b = a!; // no warning
object[] c = a! ?? throw new Exception(); // warning CS8619?! (Nullability of reference types in value of type 'object?[]' doesn't match target type 'object[]') I don't expect the warning because of the null-forgiving operator, and because the throw_expression has influence the nullability of the RHS (right?). The inferred type of the RHS is inferred from two operands: one has a type, As a workaround I found that I can assign into a temporary variable: object[] tmp = a!;
object[] d = tmp ?? throw new Exception(); But this feels awkward and it's unintuitive why the temporary variable prevents the warning. Why a warning? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The By coalescing, you’re telling the flow analysis, “Never mind what I said before, it could be nullable.” |
Beta Was this translation helpful? Give feedback.
-
This would be a smaller step than going all the way to a temporary variable: object[] c = (a ?? throw new Exception())!; It makes sense that it would work this way since the |
Beta Was this translation helpful? Give feedback.
This would be a smaller step than going all the way to a temporary variable:
It makes sense that it would work this way since the
!
is targeting the assignment, whereas in your example the!
is targeting the??
.