C#7中的var模式有什么好处?

编程入门 行业动态 更新时间:2024-10-23 19:34:38
本文介绍了C#7中的var模式有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不了解C#7中 var 模式的用例. MSDN :

与 var 模式匹配的模式始终成功.它的语法是

expr是var varname

其中,expr的值始终分配给名为的局部变量 varname . varname 是与 expr 相同类型的静态变量.

我认为MSDN上的示例几乎没有用,尤其是因为 if 是多余的:

object []项目= {新书(暴风雨"),新人(约翰")};foreach(项目中的var个项目){如果(项目是var obj)Console.WriteLine($"Type:{obj.GetType().Name},Value:{obj}"));}

在这里我看不到任何好处,如果直接访问也是 Object 类型的循环变量 item ,则可能会有相同的效果. if 也令人困惑,因为它永远不会 false .

我可以直接使用 var otherItem = item 或使用 item .有人可以更好地解释用例吗?

解决方案

var模式在 C#语言存储库,因为它不能完全清楚其用例是什么,并且考虑到 is var x 不会在 isT x 确实做到了,使其显得毫无用处.

但是,实际上并不是要使用它,因为 obj是var x .它应在左侧本身不是 变量的情况下使用.

以下是规范中的一些示例.他们都使用了C#还没有的功能,但这只是表明var模式的引入主要是为准备这些事情而准备的,因此以后不必再碰到它了./p>

以下示例声明了函数 Deriv 来构造衍生工具表达式树上使用结构模式匹配的函数的说明:

Expr Deriv(Expr e){开关(e){//…case Const(_):返回Const(0);案例Add(var Left,var Right):返回Add(Deriv(Left),Deriv(Right));//…}

在这里,可以在结构内部使用 var 模式从结构中拉出"元素.同样,以下示例简化了表达式:

Expr Simplify(Expr e){开关(e){大小写多(Const(0),_):返回Const(0);//…案例Add(Const(0),var x):返回Simplify(x);}}

gafter在这里写道,想法也是拥有财产模式匹配,允许以下操作:

如果(o是Point {X是3,Y是var y}){…}

I don't understand the use case of var patterns in C#7. MSDN:

A pattern match with the var pattern always succeeds. Its syntax is

expr is var varname

where the value of expr is always assigned to a local variable named varname. varname is a static variable of the same type as expr.

The example on MSDN is pretty useless in my opinion, especially because the if is redundant:

object[] items = { new Book("The Tempest"), new Person("John") }; foreach (var item in items) { if (item is var obj) Console.WriteLine($"Type: {obj.GetType().Name}, Value: {obj}"); }

Here i don't see any benefits, you could have the same if you access the loop variable item directly which is also of type Object. The if is confusing as well because it's never false.

I could use var otherItem = item or use item diectly. Can someone explain the use case better?

解决方案

The var pattern was very frequently discussed in the C# language repository given that it’s not perfectly clear what its use case is and given the fact that is var x does not perform a null check while is T x does, making it appear rather useless.

However, it is actually not meant to be used as obj is var x. It is meant to be used when the left hand side is not a variable on its own.

Here are some examples from the specification. They all use features that are not in C# yet but this just shows that the introduction of the var pattern was primarly made in preparation for those things, so they won’t have to touch it again later.

The following example declares a function Deriv to construct the derivative of a function using structural pattern matching on an expression tree:

Expr Deriv(Expr e) { switch (e) { // … case Const(_): return Const(0); case Add(var Left, var Right): return Add(Deriv(Left), Deriv(Right)); // … }

Here, the var pattern can be used inside the structures to "pull out" elements from the structure. Similarly, the following example simplifies an expression:

Expr Simplify(Expr e) { switch (e) { case Mult(Const(0), _): return Const(0); // … case Add(Const(0), var x): return Simplify(x); } }

As gafter writes here, the idea is also to have property pattern matching, allowing the following:

if (o is Point {X is 3, Y is var y}) { … }

更多推荐

C#7中的var模式有什么好处?

本文发布于:2023-11-03 15:47:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555488.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么好处   模式   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!