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??
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??
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);
=>
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);
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
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); }
Its a duplicate in stackoverflow : http://stackoverflow.com/questions/1415444/c-3-0-meaning-of-expression/1415470#1415470
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;
}