=> 是什么意思?运营商做什么?
我正在学习 C#/ASP.NET 教程,遇到了一个我以前从未见过的运算符。
return RSVPs.Any(r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase));
=> 什么意思?这行代码的意思是??
I am working through a C#/ASP.NET tutorial and I run into an operator i have never seen before.
return RSVPs.Any(r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase));
what does => mean in this line of code??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是定义 lambda 表达式的语法。它本质上是 C# 中委托/匿名方法的简写。
在这个特定示例中,它定义了一个委托,该委托接受 RSVP 实例,如果 attendeeName 值等于传入的用户名,则返回 true。如果传入的值相同,则 Any 扩展方法返回 true delegate 对于集合中的任何值都是 true。
这是编写您发布的示例的扩展方法
This is the syntax which defines a lambda expression. It essentially is short hand for a delegate / anonymous method in C#
In this particular example it is defining a delegate which takes an RSVP instance and returns true if the AttendeeName value equals the userName passed in. The Any extension method returns true if the passed in delegate is true for any value in the collection.
Here is an expanded way of writing the sample you posted
这是一个 lambda 表达式。在您给出的示例中,“r”是列表中的一项。此代码将迭代您的列表 [RSVP],查找“r”,其中 attendeeName 与您的用户名中的任何内容相同。
为了理解发生了什么,你需要理解委托,但本质上Coles Notes版本是 => 左边的参数; (在本例中为 r)是列表 [RSVPs] 中的一个项目,它按顺序迭代,对于 r 的每个实例,您都在检查某些内容。从用户的角度来看,这大致相当于:
This is a lambda expression. In the example you have given "r" is an item in the list. This code will iterate your list [RSVPs] looking for an "r" where the AttendeeName is the same as whatever is in your userName.
In order to understand what's going on, you need to understand delegates, but essentially the Coles Notes version is that the parameter on the left of => (r in this case) is an item in the list [RSVPs] that is being iterated through sequentially, for each instance of r, you are checking something. From a user perspective, this is vaguely equivalent to:
它定义了一个 lambda 表达式。
相当于
It defines a lambda expression.
is equivalent to
所有 lambda 表达式都使用 lambda 运算符 =>,读作“前往”。 lambda 运算符的左侧指定输入参数(如果有),右侧保存表达式或语句块。 lambda 表达式 x => x * x 读作“x 等于 x 乘以 x”。可以将此表达式分配给委托类型,如下所示:
http://msdn. microsoft.com/en-us/library/bb397687.aspx
All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:
http://msdn.microsoft.com/en-us/library/bb397687.aspx
=>
是 lambda 表达式的一部分,将其左侧的参数与其右侧的表达式/语句相关联。根据它的使用方式,它可以表示匿名委托:或等效的表达式树:
=>
is part of a lambda expression, associating the argument(s) to its left with an expression/statement to its right. Depending on how it is used, it can represent either an anonymous delegate:Or an equivalent expression tree: