当前位置:网站首页>C 11 new feature: List pattern matching
C 11 new feature: List pattern matching
2022-06-10 03:45:00 【Dotnet cross platform】
Before
Use pattern matching , You can test whether the expression result is equal to the specified constant or within a certain range :
public static string Demo(int number)
{
return number switch
{
100 => "A",
>=90 =>"B",
_ => "C"
};
}The above code , The execution logic is as follows :
When the passed in variable is equal to
100, Return resultsA;When the passed in variable is greater than or equal to
90, Return resultsB;By default , Return results
C;
however , Cannot pattern match array , For example, an element in an array is equal to 100, Return results A.
C# 11
from C# 11 Start , You can match an array or list to the pattern sequence of matching elements . Any of the following modes can be applied :
Check whether a single element matches certain characteristics
public static string Demo(int[] numbers)
{
return numbers switch
{
[100] => "A",
[>=90] => "B",
_ => "C"
};
} Must exactly match the elements of the array , For example, the array has 2 Elements can only return results C.
Abandon the meta model (_) Match with a single element
[_, 100] => "A",
[>=90, _] => "B", for example [90,100] Return results A,[90,80] Return results B.
Scope mode (..) You can match zero or more elements in a sequence . But at most one range mode in the list mode is allowed
[.., 100] => "A",
[>=90, ..] => "B", for example [80,90,100] Return results A,[90] Return results B.
var Patterns can capture a single element or multiple elements
[var number, 100,.. var array] => $"{number},{string.Join(",",array)}", for example [80,100] Return results 80,[80,100,70,60] Return results 80,70,60.
Decompile the list mode code , You can see , The code actually uses if-else Realized . List mode is just C# 11 A grammar sugar for us

Add microsignals 【MyIO666】, Invite you to join the technical exchange group
边栏推荐
- Vulnhub's doubletrouble: 1
- New function official publicity playfab makes cross platform game architecture so simple
- vulnhub之doubletrouble: 1
- 机器学习 && 内容安全 && 海外风控公司
- 【提高准确率方法总结】
- Microsoft build release - 7 major directions of technical updates concerned by developers
- [cloud native]kubernetes visual interface webui kubernetes dashboard
- Yolov5 target detection neural network -- calculation principle of loss function
- C language question brushing series (III)
- 答辩前电脑坏了......
猜你喜欢
随机推荐
Unable to start web server
使用队列实现进程之间的数据共享
Probabilistic programming tool: official introduction to tensorflow probability
Post Microsoft Build丨畅聊技术新风潮
Flowable deploy processes in three ways
【主流Nivida显卡深度学习/强化学习/AI算力汇总】
【论文笔记|深读】struc2vec: Learning Node Representations from Structural Identity
Is it the same thing to evaluate the network security level and the security of commercial password applications?
Matlab neural network
【yolov5.yaml解析】
推特同意开放数据库供马斯克核查
vulnhub之HARRYPOTTER: FAWKES
[singleshotmultiboxdetector (SSD)
【PyTorch 模型剪枝实例教程2(结构化剪枝)】
Easyexcel realizes dynamic import and export
Refactoring de duplication of code
Using the responsibility chain pattern to reconstruct the original code
关于模数转换的理解
Talk about 10 tips to ensure thread safety
[L1, L2 and smooth L1 loss functions]





![[cloud native | kubernetes] learn more about ingress](/img/b8/854f862e03241f6c2d55a901da6146.png)


![[loss calculation in yolov3]](/img/8c/1ad99b8fc1c5490f70dc81e1e5c27e.png)
