当前位置:网站首页>c#语法糖模式匹配【switch 表达式】
c#语法糖模式匹配【switch 表达式】
2022-04-23 03:02:00 【dotNET跨平台】
例子
我们首先定义一个颜色枚举
public enum ColorEnum
{
Red,
Blue,
Black,
}
我们写一个方法获取颜色的RGB值,我们这里使用Switch表达式,传统做法是:switch 语句在其每个 case 块中生成一个值,比如这样
private Color GetColorRgb(ColorEnum colorEnum)
{
switch (colorEnum)
{
case ColorEnum.Red:
return Color.FromArgb(0, 0, 0);
case ColorEnum.Blue:
return Color.FromArgb(0, 0, 0);
case ColorEnum.Black:
return Color.FromArgb(0, 0, 0);
default:
throw new ArgumentException(message: "invalid enum value", paramName: nameof(ColorEnum));
}
}
但是这样让人觉得有重复的Case,break关键字,使得代码看来不是那么的简洁,所以C#8.0中借助 Switch 表达式,可以使用更简洁的表达式语
private Color GetColorRgb(ColorEnum colorEnum)
{
return colorEnum switch
{
ColorEnum.Red => Color.FromArgb(0, 0, 0),
ColorEnum.Blue=> Color.FromArgb(0, 0, 0),
ColorEnum.Black=> Color.FromArgb(0, 0, 0),
_ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(ColorEnum));
};
}
这里有几个语法改进:
•变量位于 switch 关键字之前。不同的顺序使得在视觉上可以很轻松地区分 switch 表达式和 switch 语句。•将 case 和 : 元素替换为 =>。它更简洁,更直观。•将 default 事例替换为 _ 弃元。•正文是表达式,不是语句。我在前面的一篇文章【你真的了解Lambda吗】说过,如果最后一句是Return表达式的时候,我们呢可以把Return和{}这个关键字去掉使用=>代替!
private Color GetColorRgb(ColorEnum colorEnum)
=> colorEnum switch
{
ColorEnum.Red => Color.FromArgb(0, 0, 0),
ColorEnum.Blue => Color.FromArgb(0, 0, 0),
ColorEnum.Black => Color.FromArgb(0, 0, 0),
_ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(ColorEnum));
};
最后大家如果喜欢我的文章,还麻烦给个关注并点个赞, 希望net生态圈越来越好!
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124287006
边栏推荐
- C# 11 对 ref 和 struct 的改进
- What is the difference between varchar and char?
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
- Huawei machine test question -- deformation of hj53 Yang Hui triangle
- Traversée de l'arbre L2 - 006
- C# WPF UI框架MahApps切换主题
- Chapter IV project cost management of information system project manager summary
- If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
- Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
- PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
猜你喜欢

Innovation and management based on Scrum

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)

最通俗易懂的依赖注入之服务容器与作用域

腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元

AOT和单文件发布对程序性能的影响

Source code and some understanding of employee management system based on polymorphism

Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

Windows MySQL 8 zip installation

Q-Learning & Sarsa
随机推荐
Encapsulation of ele table
Navicat premium import SQL file
基于ele封装下拉菜单等组件
Redis data server / database / cache (2022)
Dynamic sequence table + OJ
JS relearning
tf. keras. layers. Embedding function
Traversée de l'arbre L2 - 006
The difference between encodeuri and encodeuricomponent
The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
Microservices (distributed architecture)
Array and collection types passed by openfeign parameters
c#可变参数params的介绍
Shell learning notes -- shell processing of output stream awk
Introduction and use of openfeign component
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
Chapter VII project communication management of information system project manager summary
Use of MySQL command line client and common commands