3 ms·
C# 8 also has "switch expressions" (1) meaning that all those "return" keywords fall away so this is valid (although useless) c# 8: public static bool Inv
by SideburnsOfDoom 7y ago
C# 8 also has "switch expressions" (1) meaning that all those "return" keywords fall away
so this is valid (although useless) c# 8:
public static bool Invert(bool value) =>
value switch
{
true => false,
false => true
};
I worry though that the "switch" syntax is becoming a big complex monster, and can be used to make utterly unreadable code, rather than simplifying as promised. We'll probably see both in practice.
It might have been nicer if they could have used a new keyword e.g. "match" to carry the new syntax, rather than overloading "switch", but that would not be backwards compatible with C# 1-7, as "match" was not a reserved word. (2)
Hopefully switch the syntax that supports these various cases doesn't get too tricky.
1) https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
2) https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...