当前位置:网站首页>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
边栏推荐
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
- Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
- MAUI初体验:爽
- The space between the left and right of the movie ticket seats is empty and cannot be selected
- Summary of software test interview questions
- Using stack to solve the problem of "mini parser"
- 交换二叉树中每个结点的左和右
- How to count the number of all files in a directory under win10 system
- Onenet connection process
- [new version release] componentone added Net 6 and blazor platform control support
猜你喜欢

Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel

The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details

It turns out that PID was born in the struggle between Lao wangtou and Lao sky

Traversée de l'arbre L2 - 006
![FileNotFoundError: [Errno 2] No such file or directory](/img/ea/0c3f2768d14c1f4bb42bd1309ab996.png)
FileNotFoundError: [Errno 2] No such file or directory

Er and eer models

Q-Learning & Sarsa

Opencv combines multiple pictures into video

ASP.NET 6 中间件系列 - 自定义中间件类

Source Generator实战
随机推荐
TP5 inherits base and uses the variables in base
Blazor University (11) component - replace attributes of subcomponents
REINFORCE
Blazor University (12)组件 — 组件生命周期
Numpy stack function
微软是如何解决 PC 端程序多开问题的
使用split来解决“最常见的单词”问题
How to count the number of all files in a directory under win10 system
Detailed log display of openfeign call
Judge whether there is a leap year in the given year
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
C# 11 的这个新特性,我愿称之最强!
Er and eer models
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
Dynamic sequence table + OJ
[new version release] componentone added Net 6 and blazor platform control support
Face longitude:
SQL statement - DDL
Close the computer port
c#可变参数params的介绍