What does the => operator do?

I am working on a C # / ASP.NET tutorial and I am running into an operator that I have never seen before.

return RSVPs.Any(r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase));

      

what does => mean in this line of code?

+2


source to share


5 answers


This is a lambda expression. In the example you provided, "r" is an item in the list. This code will iterate over your [RSVPs] list looking for "r" where AttendeeName matches what's in your username.

To understand what's going on, you need to understand delegates, but essentially the Coles Notes version is that the parameter to the left of => (r in this case) is an item in the [RSVPs] list, which is repeated 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;
}

      

+2


source


This is the syntax that defines a lambda expression. This is essentially a shorthand 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 defines a delegate that accepts an RSVP instance and returns true if the AttendeeName is equal to the passed username. Any extension method returns true if the passed in the delegate is true for any value in the collection.



Here is an advanced way of writing your laid out sample

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

      

+3


source


Defines a lambda expression .

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

      

equivalent to

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

      

+2


source


All lambda expressions use the lambda operator =>, which reads "goes". The left side of the lambda operator indicates the input parameters (if any), and the right side contains the expression or statement block. The lambda expression x => x * x is read "x goes x times x". This expression can be assigned to the 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

      

+1


source


=>

is part of the lambda expression, matching the argument to its left with / statement to its right. Depending on how it is used, it can be 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);

      

0


source







All Articles