tags:

views:

162

answers:

6

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??

+3  A: 

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);
JaredPar
Is there a reason this is better than a foreach loop that exits once it finds a match?I this used for code readability? speed?
Lumpy
If the OP's only just coming across "=>" then perhaps your Func<int,int> needs some explanation too. I use Lambdas all the time, but I have to admit that when I start seeing Funcs introduced, I start getting flustered.
BobTheBuilder
A: 

=> 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);
dahlbyk
+1  A: 

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
Chris Pietschmann
I think "goes to" is a little misleading in the context of the example, it's more like the SQL "Select ListItem where ListItem.AttendeeName = userName"
BobTheBuilder
Take it up with MSDN, since I just copied and pasted from the documentation. :)
Chris Pietschmann
+2  A: 

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); }
stevemegson
A: 

Its a duplicate in stackoverflow : http://stackoverflow.com/questions/1415444/c-3-0-meaning-of-expression/1415470#1415470

anishmarokey
You should add this as a comment to the question.
Callum Rogers
ohh sorry :( . next time i will do that ...
anishmarokey
+2  A: 

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;
}
BobTheBuilder
Is there a reason to use the lambda expression instead of the code you gave. Your code is much easier to read, for me anyway.
Lumpy
Admittedly that for a user that is new to lambda expressions and delgates, my code seems easier to read. However once you get used to throwing delegates and lambda expressions around (which will come in time) it will surprise you that lambda expressions actually become more intuitive and thus easier to read than the longhand version.
BobTheBuilder
I don't know if you have any database experience, but this is like saying: return Exists(Select r From RSVPs Where r.AttendeeName = @userName); Lambda expressions give you power to be more expressive in a smaller amount of code.
BobTheBuilder
@Daniel Auger - I agree, the fewer lines of code you write, the fewer bugs you can introduce.
BobTheBuilder
They definitely cut down the total lines of code. Also, it can be argued that using lambdas with proven/reliable extension methods is potentially less error prone than doing it long hand. They do seem weird at first, but they become 2nd nature quickly.
Daniel Auger