关于 C# 8.0 的 Switch Case When 的用法
2019-06-30 15:16 音乐让我说 阅读(10971) 评论(0) 收藏 举报直接贴代码了:
static void Main(string[] args) { SwitchSample(); } private static void SwitchSample() { Switch(new Circle { Radius = 10 }); Switch(new Rectangle { Height = 10, Width = 10 }); Switch(new Rectangle { Height = 20, Width = 30 }); } private static void Switch(object o) { switch (o) { case Circle c: Console.WriteLine($"it's a circle with radius {c.Radius}"); break; case Rectangle r when (r.Width == r.Height): Console.WriteLine($"it's a square with width {r.Width}"); break; case Rectangle r: Console.WriteLine($"it's a rectangle with width {r.Width} and height {r.Height}"); break; default: break; } }
运行截图:

谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
浙公网安备 33010602011771号