=> 是什么意思?运营商做什么?

发布于 2024-08-08 10:05:53 字数 205 浏览 3 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦罢 2024-08-15 10:05:53

这是定义 lambda 表达式的语法。它本质上是 C# 中委托/匿名方法的简写。

Func<int,int> add2 = x => x + 2;
int value = add2(42); // value == 44

在这个特定示例中,它定义了一个委托,该委托接受 RSVP 实例,如果 attendeeName 值等于传入的用户名,则返回 true。如果传入的值相同,则 Any 扩展方法返回 true delegate 对于集合中的任何值都是 true。

这是编写您发布的示例的扩展方法

Func<RSPV,bool> del = r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
return RSVPS.Any(del);

This is the syntax which defines a lambda expression. It essentially is short hand for a delegate / anonymous method in C#

Func<int,int> add2 = x => x + 2;
int value = add2(42); // value == 44

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

Func<RSPV,bool> del = r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
return RSVPS.Any(del);
有木有妳兜一样 2024-08-15 10:05:53

这是一个 lambda 表达式。在您给出的示例中,“r”是列表中的一项。此代码将迭代您的列表 [RSVP],查找“r”,其中 attendeeName 与您的用户名中的任何内容相同。

为了理解发生了什么,你需要理解委托,但本质上Coles Notes版本是 => 左边的参数; (在本例中为 r)是列表 [RSVPs] 中的一个项目,它按顺序迭代,对于 r 的每个实例,您都在检查某些内容。从用户的角度来看,这大致相当于:

public bool HasRSVP(List<RSVP> RSVPs, string userName)
{
    foreach(RSVP r in RSVPs)
        if(r.AttendeeName.Equals(userName, StringComparison.InvariantCulture))
            return true;
    return false;
}

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:

public bool HasRSVP(List<RSVP> RSVPs, string userName)
{
    foreach(RSVP r in RSVPs)
        if(r.AttendeeName.Equals(userName, StringComparison.InvariantCulture))
            return true;
    return false;
}
风筝在阴天搁浅。 2024-08-15 10:05:53

它定义了一个 lambda 表达式

r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase)

相当于

delegate( RSVP r ) { return r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase); }

It defines a lambda expression.

r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase)

is equivalent to

delegate( RSVP r ) { return r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase); }
笙痞 2024-08-15 10:05:53

所有 lambda 表达式都使用 lambda 运算符 =>,读作“前往”。 lambda 运算符的左侧指定输入参数(如果有),右侧保存表达式或语句块。 lambda 表达式 x => x * x 读作“x 等于 x 乘以 x”。可以将此表达式分配给委托类型,如下所示:

delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}

http://msdn. microsoft.com/en-us/library/bb397687.aspx

Func<int, bool> myFunc = x => x == 5;
bool result = myFunc(4); // returns false of course

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:

delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}

http://msdn.microsoft.com/en-us/library/bb397687.aspx

Func<int, bool> myFunc = x => x == 5;
bool result = myFunc(4); // returns false of course
白芷 2024-08-15 10:05:53

=> 是 lambda 表达式的一部分,将其左侧的参数与其右侧的表达式/语句相关联。根据它的使用方式,它可以表示匿名委托:

return RSVPs.Any(delegate (RSVP r) { r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase) });

或等效的表达式树:

Expression<Func<RSVP, bool>> e = r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase);

=> 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:

return RSVPs.Any(delegate (RSVP r) { r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase) });

Or an equivalent expression tree:

Expression<Func<RSVP, bool>> e = r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文