Try this syntax too:
var info = age switch
{
10 or 20 => "U",
_ => "A"
};
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In C# 8.0 switch enhancements, how do we perform below matches with new version?
int age = 20;
switch (age)
{
case 10:
case 20:
return "U";
default: return "A";
}
with new switch syntax in C# 8.0 how to handle above one?
var info = age switch {
10 => "U",
20 => "U",
_ => "A"
}
Is there any better way to handle multiple values?
Try this syntax too:
var info = age switch
{
10 or 20 => "U",
_ => "A"
};
In C# 8, you can do :
var info = age switch
{
var n when n == 10 || n == 20 => "U",
_ => "A"
};
(or in C# 9 instead of when)