Can you tell me what this ( => ) means, in the following context?
IEnumerablequery = names .Where(s => s.Length == 5) .OrderBy(s => s) .Select(s => s.ToUpper());
Can you tell me what this ( => ) means, in the following context?
IEnumerablequery = names .Where(s => s.Length == 5) .OrderBy(s => s) .Select(s => s.ToUpper());
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Feb 18, 2008, 12:36 PM
Hi Michael,
Yes, I am reasonably well versed in LINQ but that's only because I've been following it for the last couple of years whilst it's been in development so I was able to hit the ground running when VS 2008 came out.
There's plenty of resources on LINQ over at the .NET Framework developer center:
http://msdn2.microsoft.com/en-gb/netframework/aa904594.aspx
One thing you do need to get used to now is the functional style of programming which lambda expressions entail but is rather foreign to most C# developers including myself.
Matthew Cochran has written a number of excellent articles about functional programming using C# on this site:
http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorID=rmcochran
I do know what you mean about it being difficult to keep up, with MS throwing new stuff at us every other week - or so it seems :)
Michal MoorePosted Feb 18, 2008, 11:43 AM
Alan
are you well versed in LINQ and all this, already?
I find it nearly impossible to keep up.
Michal MoorePosted Feb 18, 2008, 11:42 AM
thanks Alan... I could not find anything in google that referred to =>
but after reading your response, I used, lambda expression operator and found this
http://msdn2.microsoft.com/en-us/library/bb397687.aspx
thanks very much!
AlanPosted Feb 18, 2008, 10:21 AM
=> is the operator used for creating lambda expressions in C# 3.0.
A lambda expression is similar to (but more powerful than) an anonymous method which was introduced in C# 2.0. In other words it's a function which is written 'inline' and can be assigned to a compatible delegate.
What's at the LHS of the => is the parameter (or parameters) of the function and what's at the RHS is the return value of the function. It's also possible to have a block of statements rather than just an expression on the RHS.
Unless you're already familiar with other features of C# 3.0 such as extension methods and type inference, it's difficult to explain how that code works but what it does is to examine each string, s, in the string collection, names, and return a query consisting of all such strings, whose length is 5, converted to upper case and in alpahabetical order.