The switch statement is ready for a syntactical sugar overhaul. Or at least, an alternative for those who prefer one. And .NET is a prime candidate to be a leader in this arena.
```c#
switch (vehicleType)
{
VehicleType.Truck
{
//logic
}
VehicleType.Car
{
//logic
}
VehicleType.EighteenWheeler,
VehicleType.MegaHauler
{
//BIG logic
}
VehicleType.CompactCar
return "CompactCar";
default
{
throw new NotSupportedException("I don't know what it is you're drivin, mate.");
}
}
Compare this to the current switch syntax:
```c#
switch (vehicleType)
{
case VehicleType.Truck:
//logic
break;
case VehicleType.Car:
//logic
break;
case VehicleType.EighteenWheeler:
case VehicleType.MegaHauler:
//BIG logic
break;
case VehicleType.CompactCar:
return "CompactCar";
default:
throw new NotSupportedException("I don't know what it is you're drivin, mate.");
}
While my first example showing the new syntax is more lines, it is, in my opinion, cleaner, easier to read, uses fewer specialized keywords, less bug-prone, and sticks with the already understood notion that brackets in C# mean scope.
Keyword and special character usage by example:
(New Syntax) = switch { } ( ) default ; ,
(Old Syntax) = switch { } ( ) default ; case break :
While I am not proposing to eliminate the old syntax, I am asking that this alternative be made available for those who wish to take advantage of it. Again, it is sugar, but there have been many sugar-coated updates recently to the .NET Framework and .NET Core. This is not that far out there of an idea.
.NET is constantly evolving as a language. I think it is time to switch differently.
Love, Ryan C
C# 8.0 is already proposed to add the switch expression. I don't think a third form of switch would be a good idea.
Also, suggestions to change the C# language belong on the csharplang repo.
While I would like not having to write break; all the time and really appreciate swift's fallthrough statement, I also don't think having a third option would be too much.
Please move the issue to https://github.com/dotnet/csharplang repo.
Personally, I don't think it makes sense to add multiple syntaxes to achieve the same thing. But I am not C# designer, so I let them make the final call.
Also note that addition of each feature creates complexity and interaction with other features.
Thanks all! Moved to csharplang!
Most helpful comment
C# 8.0 is already proposed to add the
switchexpression. I don't think a third form ofswitchwould be a good idea.Also, suggestions to change the C# language belong on the csharplang repo.