It′s mid-fall which means a new version of C# is coming soon. It′s time to find out what updates will soon appear in the language. Although C#12 has fewer features than previous versions, it still...
I’m not sure I understand your point about fall through having to be explicit
As far as i understand it, every switch statement requires a break otherwise it’s a compiler error - which makes sense from the “fallthrough is a footgun” C perspective. But fallthrough isnt the implicit behavior in C# like it is in C - the absence of a break wouldnt fall through, even if it wasnt a compiler error. Fallthrough only happens when you explicitly use goto.
But break is what you want 99% of the time, and fallthrough is explicit. So why does break also need to be explicit? Why isnt it just the default behavior when there’s nothing at the end of the case?
It’s like saying “my hammer that’s on fire isnt safe, so you’re required to wear oven mitts when hammering” instead of just… producing a hammer that’s not on fire.
From what i saw on the internet, the justification (from MS) was literally “c programmers will be confused if they dont have to put breaks at the end”.
Ah ok I think I get you now. To be clear, fall through is implicit - when the case being fallen through is empty. I forgot that, if you want to execute some statements in one case, and then go to another case, you need gotos. To be fair, I’ve never needed that behavior before.
I absolutely see your point on break not being the default. It is sad, although I will say I don’t mind a little extra explicitness in code I’m sharing with a large team.
I mean if your issue is with the break pattern you could just switch to using a switch expression instead of a statement, it’ll probably be a little cleaner for ya
As far as i understand it, every switch statement requires a
break
otherwise it’s a compiler error - which makes sense from the “fallthrough is a footgun” C perspective. But fallthrough isnt the implicit behavior in C# like it is in C - the absence of abreak
wouldnt fall through, even if it wasnt a compiler error. Fallthrough only happens when you explicitly usegoto
.But
break
is what you want 99% of the time, and fallthrough is explicit. So why doesbreak
also need to be explicit? Why isnt it just the default behavior when there’s nothing at the end of the case?It’s like saying “my hammer that’s on fire isnt safe, so you’re required to wear oven mitts when hammering” instead of just… producing a hammer that’s not on fire.
From what i saw on the internet, the justification (from MS) was literally “c programmers will be confused if they dont have to put breaks at the end”.
Ah ok I think I get you now. To be clear, fall through is implicit - when the case being fallen through is empty. I forgot that, if you want to execute some statements in one case, and then go to another case, you need gotos. To be fair, I’ve never needed that behavior before.
I absolutely see your point on break not being the default. It is sad, although I will say I don’t mind a little extra explicitness in code I’m sharing with a large team.
That’s even worse… why isnt an empty case a syntax error?
I mean if your issue is with the break pattern you could just switch to using a switch expression instead of a statement, it’ll probably be a little cleaner for ya
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression
Edit: sorry just realized y’all already talked about this :p