Pattern Matching with the switch Statement

static void SwitchStatement(object item)
{
switch (item)
{
case null:
case 42:
Console.WriteLine("it's a const pattern");
break;
case int i:
Console.WriteLine($"it's a type pattern with int: {i}");
break;
case string s:
Console.WriteLine($"it's a type pattern with string: {s}");
break;
case Person p when p.FirstName == "Katharina":
Console.WriteLine($"type pattern match with Person and " +
$"when clause: {p}");
break;
case Person p:
Console.WriteLine($"type pattern match with Person: {p}");
break;
case var every:
Console.WriteLine($"var pattern match: {every?.GetType().Name}");
break;
default:
}
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读