C# 9: New `and`, `or`, and `not` Keywords for Pattern Matching

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

内容简介:Though it may sound like our occasionalApril Fools Day joke, C# 9 is looking to addIn order to make pattern matching more flexible and powerful, C#’s designers want to add the concept of conjunctive, disjunctive, and negated patterns. Superficially, they l

Though it may sound like our occasionalApril Fools Day joke, C# 9 is looking to add and , or , and not to its list of keywords. Specifically, for use in pattern matching.

In order to make pattern matching more flexible and powerful, C#’s designers want to add the concept of conjunctive, disjunctive, and negated patterns. Superficially, they look just like Boolean operations where you want to match on both patterns (conjunctive), either pattern (disjunctive) or not match on a pattern (negated).

And that’s the problem. If you are working with Booleans, the && and || operators would be ambiguous; the compiler wouldn’t be able to determine if they are referring to values or patterns. To illustrate this idea, consider this disjunctive pattern:

if (myBool is true or false)

This would be interpreted as “true if myBool equals true or if myBool equals false”.

If we use the && and || operators for combining patterns, then we get this syntax:

if (myBool is true || false)

But this statement already means “true if myBool equals the result of (true or false)”. Or to simplify it, “true if myBool equals true”. Which is a completely different result than what we’d get with above disjunctive pattern.

Hence the new and, or, and not keywords are necessary to avoid ambiguity. You can learn more about them in Champion "and, or, and not patterns" . In the C# 9 feature status they are marked as being merged into the master branch.

One question frequently raised is whether or not the same variable can be declared multiple times. For example,

if ((e1, e2) is (0, int x) or (int x, 0))
{
    M(x);
}

In this theoretical example, the variable x is either e1 or e2 . This would be the equivalent of writing,

case (0, int x):
case (int x, 0):
    M(x);

In fact, you would have to declare x in both patterns for x to be definitely assigned . If you used the below code instead, either x or y wouldn’t have a value.

if ((e1, e2) is (0, int x) or (int y, 0))
{
    M(x);
    M(y);
}

In order to this problem, the current proposal says,

beneath a not or or, pattern variables may not be declared.

This may be reconsidered in future versions of C#.


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

查看所有标签

猜你喜欢:

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

服务设计与创新实践

服务设计与创新实践

宝莱恩 (Andy Polaine)、乐维亚 (Lavrans Lovlie)、里森 (Ben Reason) / 王国胜、张盈盈、付美平、赵芳 / 清华大学出版社 / 2015-6-1 / CNY 69.00

产品经济的时代渐行渐远,在以服务为主导的新经济时代,在强调体验和价值的互联网时代,如何才能做到提前想用户之所想?如何比用户想得更周到?如何设计可用、好用和体贴的服务?这些都可以从本书中找到答案。本书撷取以保险业为代表的金融服务、医疗服务、租车及其他种种服务案例,从概念到实践,有理有据地阐述了如何对服务进行重新设计?如何将用户体验和价值提前与产品设计融合在一起? 《服务设计与创新实践》适合产品......一起来看看 《服务设计与创新实践》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具