当前位置:网站首页>C syntax pattern matching [switch expression]
C syntax pattern matching [switch expression]
2022-04-23 03:06:00 【Dotnet cross platform】
Example
We first define a color enumeration
public enum ColorEnum
{
Red,
Blue,
Black,
}
Let's write a method to get the color RGB value , Here we use Switch expression , The traditional way is :switch Statement in each of its case Generate a value in the block , Such as this
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));
}
}
But this makes people feel repetitive Case,break keyword , Makes the code look less concise , therefore C#8.0 With the help of Switch expression , You can use more concise expressions
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));
};
}
Here are a few grammar improvements :
• The variable is located in switch Before keywords . The different order makes it visually easy to distinguish switch Expression and switch sentence .• take case and : The element is replaced with =>. It's simpler , More intuitive .• take default Replace the case with _ Abandon yuan .• The body is an expression , It's not a statement . My previous article 【 Do you really understand Lambda Do you 】 Said , If the last sentence is Return In terms of expression , We can Return and {} Remove this keyword and use => Instead of !
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));
};
Finally, if you like my article , Please pay attention and praise , hope net The ecosystem is getting better and better !
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334154.html
边栏推荐
- Q-Learning & Sarsa
- Use split to solve the "most common words" problem
- tf. keras. layers. Inputlayer function
- PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
- The difference between encodeuri and encodeuricomponent
- Array and collection types passed by openfeign parameters
- FileNotFoundError: [Errno 2] No such file or directory
- tf. keras. layers. MaxPooling? D function
- Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
- LNMP MySQL allows remote access
猜你喜欢
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
Summary of software test interview questions
Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
Array and collection types passed by openfeign parameters
Source Generator实战
AC & A2C & A3C
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Introduction to ACM [TSP problem]
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
随机推荐
ASP.NET 6 中间件系列 - 自定义中间件类
TP5 where query one field is not equal to multiple values
使用两种方法来解决“最大回文数乘积”问题
7-11 rearrange the linked list (25 points)
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
[format] simple output (2)
Using stack to solve the problem of "mini parser"
【鉴权/授权】自定义一个身份认证Handler
Opencv combines multiple pictures into video
Dynamic sequence table + OJ
一套关于 内存对齐 的C#面试题,做错的人很多!
[ncnn] - the meaning of - 23300 in param
Detailed explanation of distributed things
The difference between encodeuri and encodeuricomponent
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
AOT和单文件发布对程序性能的影响
tf. keras. layers. Embedding function
The space between the left and right of the movie ticket seats is empty and cannot be selected