throw expression in null coalescing assignment #4757
Answered
by
333fred
ronnygunawan
asked this question in
Language Ideas
-
Why is this not allowed? void F(string? s) {
s ??= throw new ArgumentNullException(nameof(s));
} Currently this code gives me an error: |
Beta Was this translation helpful? Give feedback.
Answered by
333fred
May 18, 2021
Replies: 3 comments
-
I'd conjecture it's not allowed because there's nothing assignable specified on the righthand side of the assignment. I'd further suggest this is deliberate, not an oversight. |
Beta Was this translation helpful? Give feedback.
0 replies
-
But you could write: using System;
void F(string? s) {
_ = s ?? throw new ArgumentNullException(nameof(s));
}
F(null); |
Beta Was this translation helpful? Give feedback.
0 replies
-
Duplicate of #2091. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
333fred
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Duplicate of #2091.