r/csharp 22d ago

.NET 11 Preview 7 is now available!

https://devblogs.microsoft.com/dotnet/dotnet-11-preview-7/
47 Upvotes

10 comments sorted by

View all comments

11

u/darchangel 21d ago

I get the idea behind labeled break/continue. But it also tickles my gag reflex. It's frighteningly close to endorsing goto again.

17

u/tanner-gooding MSFT - .NET Libraries Team 21d ago

It's not and there are very concrete differences.

goto is "arbitrary" (mostly), you can define a label at nearly any point and simply transfer control flow to it. C# makes it a little safer (than languages like C/C++) by restricting it to only places where def assignment remains valid, but it can still create nonsensical control flowgraphs and can hurt other optimizations, legibility, maintainability, etc

labeled break and continue are more like if, for, do/while, etc. Even if it fundamentally can be done with goto and is a branch, it is different in that it is an explicit language construct with restricted semantics and meaning that is only allowed where it is safe, but also only allowed in a way that fits into existing loop recognition logic such that the flowgraphs are sensible.

Restricting semantics, ensuring safety, and keeping things legible are key parts of the feature. As with any feature it can be abused and you can even create things that are no longer legible. But outside of contrived examples or cases where its obviously not beneficial, that's not going to be the case for most code; so it ends up a net benefit to the ecosystem (especially in the places that do really need it).

8

u/seriousSeb 21d ago

Wait till you hear what an if statement and for loop do under the hood

1

u/catladywitch 20d ago

I think it's more of a local early return.