C# 9: Range Operators in Switch Constructs and Pattern Matching Expressions

栏目: IT技术 · 发布时间: 4年前

内容简介:Since C# was first introduced, developers have complained about the lack of a range operator in switch constructs. This made switches far less useful in C# than they were in VB. As part of theIn this feature, the following patterns will be allowed followin

Since C# was first introduced, developers have complained about the lack of a range operator in switch constructs. This made switches far less useful in C# than they were in VB. As part of the pattern matching enhancements for C# 9 , this limitation has been addressed.

In this feature, the following patterns will be allowed following the case or is keyword.

  • < constant-expression
  • constant-expression
  • <= constant-expression
  • >= constant-expression
  • constant-expression to constant-expression

This is still more limited than VB in the sense that only constants are allowed. This becomes a problem when you are dealing with dates, times, or other comparable structs, as they do not have a constant representation in C#. (Date/time values can be expressed as a constant in VB, but it is discouraged because one runs into DateTime.Kind issues.) When asked why, Neal Gafter wrote ,

It doesn't do the same thing that pattern-matching was designed to do. Specifically, it was designed to work with constants specifically so that the compiler can analyze, diagnose, and optimize the totality of the set of pattern-matching operations. If the compiler doesn't know the value it is matching against, it cannot do any of that. That's not to say it is necessarily a bad idea, but it is a pretty different animal. Do ordinary expressions not satisfy that need?

Mariusz Pawelski explains why this may be a problem,

I just think there will be many people who will see using is and or keywords as just a different way of writing boolean expressions. Just another small C# feature that makes code more succinct. Similar to tons of other we got in recent years ( expression-bodied members, out variables, tuples, null-conditional operators operator, "default" literal, etc). They won't know it's part of greater feature like "pattern-matching" which "was designed to work with constants". They will just be confused when instead of constant they will use variable name and get "CS0150: A constant value is expected" error.

In our previous article, we discussed the and, or, and not pattern matching operators . These can be used in conjunction with ranges. For example,

bool IsLetter(char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';

Admittedly, this is a bit difficult to read. So, another feature included in C# 9 is the ability to use parenthesis around patterns just as you would for other types of expressions to make the precedence explicit.

bool IsLetter(char c) => c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z');

This can be further simplified using inclusive ranges ( to ):

bool IsLetter(char c) => c is ('a' to 'z') or ('A' to 'Z');

According to the C# 9 feature status , these features are marked as having been merged into the master branch.


以上所述就是小编给大家介绍的《C# 9: Range Operators in Switch Constructs and Pattern Matching Expressions》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

以匠心,致设计:网易 UEDC 用户体验设计

以匠心,致设计:网易 UEDC 用户体验设计

网易用户体验设计中心 / 电子工业出版社 / 2018-8 / 95.00元

为什么网易云音乐的体验流畅细腻、富有温度?为什么网易严选App的UI设计让人感到温馨、舒适?为什么网易蜗牛读书App的界面设计让用户爱不释手…… 《以匠心,致设计:网易 UEDC 用户体验设计》是网易用户体验设计团队对自身过去的设计思考的精心总结,最大程度还原了网易产品背后的设计故事,内容涵盖设计基础、设计实践、方法思考、成长指南四章,借助网易产品设计的实际案例具体讲述了设计师日常工作中不可......一起来看看 《以匠心,致设计:网易 UEDC 用户体验设计》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具