Linq =>

When we do things like Max in Linq we use => - See attached code example

What does this => actually mean though?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace LinqExamples
{
    public class Program
    {
        static void Main(string[] args)
        {
            Person dave = new Person { FirstName = "Dave", LastName = "Amour", Age = 42 };
            Person fred = new Person { FirstName = "Fred", LastName = "Smith", Age = 22 };
            Person tiff = new Person { FirstName = "Tiff", LastName = "Bloggs", Age = 24 };
            Person kate = new Person { FirstName = "Kate", LastName = "Brown", Age = 33 };
            Person bill = new Person { FirstName = "Bill", LastName = "Green", Age = 38 };
            Person ian = new Person { FirstName = "Ian", LastName = "Smith", Age = 19 };
            Person fiona = new Person { FirstName = "Fiona", LastName = "Williams", Age = 28 };
 
            Person[] people = new Person[7];
 
            people[0] = dave;
            people[1] = fred;
            people[2] = tiff;
            people[3] = kate;
            people[4] = bill;
            people[5] = ian;
            people[6] = fiona;
 
            Console.WriteLine(people.Max(p => p.Age));
 
            Console.Read();
        }
    }
 
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }
}

Open in new window

LVL 19
daveamourAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
=> is a lambda expresison

As you can see, you don't have to explicitly declare a new method to link it with an event. You can use anonymous methods to achieve the same thing in C# 2.0. C# 3.0 introduces an even simpler syntax, lambda expressions, which you write as a parameter list followed by the "=>" token, followed by an expression or a statement block.
http://www.developer.com/net/csharp/article.php/3598381
0
daveamourAuthor Commented:
Sorry didn't understand any of that in relation my question.
0
Richard LeeSoftware EnthusiastCommented:
Max accepts a delegate that takes one parameter. Using the lambda expression is a short way to create and anonymous method (to satisfy the delegate parameter requirements) and pass it to the method Max.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

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

http://msdn.microsoft.com/en-us/library/bb397687.aspx
0
Rahul Goel ITILSenior Consultant - DeloitteCommented:
It is like inline statement, using this you no need to create a delagate method for predicate. for example we need to write this way

people.Max(delegate(Person p){
// Do Operations
return p.Age;
});

Now we use inline like this, this is called lambda expression using lambda operator =>
people.Max(p => p.Age);

It saves lines of code, improves the performance, less memory objects

I hope I made clear to u
0
daveamourAuthor Commented:
Ok so does this sound right for my code example above...
The Max method is available through extension methods defined against the IEnumerable interface,
The Max method takes an argument which is an anonymous method in the form of a lamda expression (which is just a more concise form of writing simple anonymous methods).
Somewhere in the code of the Max method as written by Microsoft  it will push each item in the collection through this anonymous method to figure out which property is used to perform the max calculation.
Within the lamda expression the => is read out as "goes to" - ie p goes to p.Age
Does that sound about right?
0
Richard LeeSoftware EnthusiastCommented:
The Max method is an extension method of the IEnumerable interface,

The Max method takes an argument which is a delegate. This parameter can be supplied using a lambda expression which is just a more concise form of writing a simple anonymous methods.

Somewhere in the code of the Max method as written by Microsoft it will push each item in the collection through this method to perform the Max calculation.

When passing the delegate as a parameter to the Max method one of the overloads of the Max extension method will have to be satisfied - this is to say that if you satisfy the delegate which take and integer then the "greater than >"  or "less than <" operator will be used against the integer.

Within the lamda expression the => is read out as "goes to" - ie p goes to p.Age

Is this good?
0
daveamourAuthor Commented:
Is this good?
I hope so but that was my question!
0
Richard LeeSoftware EnthusiastCommented:
I rewrote your question to be a bit more precise and concluding that this is how it works
0
Richard LeeSoftware EnthusiastCommented:
I have rewritten the question to be more accurate. I also added a revision for your final example.

---------

The Max method is an extension method of the IEnumerable interface,

The Max method takes an argument which is a delegate. This parameter can be supplied using a lambda expression which is just a more concise form of writing a simple anonymous methods.

Somewhere in the code of the Max method as written by Microsoft it will push each item in the collection through this method to perform the Max calculation.

When passing the delegate as a parameter to the Max method one of the overloads of the Max extension method will have to be satisfied - this is to say that if you satisfy the delegate which take and integer then the "greater than >"  or "less than <" operator will be used against the integer.

Within the lamda expression the => is read out as "goes to"

example:

p => p.Age

or

delegate int del(people p)
{
  return p.Age
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
daveamourAuthor Commented:
Ok thanks that's great and I'll accept that as an answer.
One other question though if you don't mind.
In this link which someone posted, the anonymous delegate does not adhere to the delegate signature of the click event.  How is it we are able to do this?
http://www.developer.com/net/csharp/article.php/3598381 
0
daveamourAuthor Commented:
Thanks
0
daveamourAuthor Commented:
I'm going to post this last one as a seperate question.
0
Richard LeeSoftware EnthusiastCommented:
See the example below; I provide an excerpt

http://www.csharp-station.com/Tutorials/Lesson21.aspx

Using Delegate Parameters with Anonymous Methods

Many event handlers need to use the parameters of the delegate they are based on. The previous example didn't use those parameters, so it was more convenient to not declare them....
0
daveamourAuthor Commented:
Ok so its built into the compiler that when using anonymous delegates that you don't have to code the method signature if you aren't using the parameters.  If you do code a signature in your anonymous method though then it does have to match the delegate definition.
Am I right in thinking this relaxation only applies to events as I couldn't get the attached code to work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace LinqExamples
{
    class LambdaTest
    {
        delegate void del(int i);
 
        static void Main()
        {
            del m = delegate() { Console.WriteLine("Hello"); };
 
            m();
 
            Console.Read();
        }
    }
}

Open in new window

0
Richard LeeSoftware EnthusiastCommented:
When calling delegates you have to supply the parameter however when constructing anonymous methods you don't have to declare parameter if not used.

See code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace LinqExamples
{
    class LambdaTest
    {
        delegate void del(int i);
 
        static void Main()
        {
            del m = delegate { Console.WriteLine("Hello"); };
 
            m(2);
 
            Console.Read();
        }
    }
}

Open in new window

0
daveamourAuthor Commented:
Ok right I get it now.
Thanks very much,
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.