What does the '=>' syntax in C# mean?

C#Syntax

C# Problem Overview


I just came over this syntax in some of the questions in this forum, but Google and any other searchengine tends to block out anything but letters and number in the search so it is impossible to search out "=>".

So can anyone tell me what it is and how it is used?

C# Solutions


Solution 1 - C#

It's the lambda operator.

From C# 3 to C# 5, this was only used for lambda expressions. These are basically a shorter form of the anonymous methods introduced in C# 2, but can also be converted into expression trees.

As an example:

Func<Person, string> nameProjection = p => p.Name;

is equivalent to:

Func<Person, string> nameProjection = delegate (Person p) { return p.Name; };

In both cases you're creating a delegate with a Person parameter, returning that person's name (as a string).

In C# 6 the same syntax is used for expression-bodied members, e.g.

// Expression-bodied property
public int IsValid => name != null && id != -1;

// Expression-bodied method
public int GetHashCode() => id.GetHashCode();

See also:

(And indeed many similar questions - try the lambda and lambda-expressions tags.)

Solution 2 - C#

It's a much more concise form of method notation. The following are roughly equivalent:

// explicit method
int MyFunc(int pParam) {
   return pParam;
}

// anonymous (name-less) method
// note that the method is "wrapped" up in a hidden object (Delegate) this way
// so there is a very tiny bit of overhead compared to an explicit method
// (though it's really the assignment that causes that and would also happen
// if you assigned an explicit method to a reference)
Func<int, int> MyFunc = delegate (int pParam) { return pParam; };

// lambda expression (also anonymous)
// basically identical to anonymous method,
// except with everything inferred as much as possible, intended to be minimally verbose
Func<int, int> MyFunc = x => x;

// and => is now also used for "expression-bodied" methods
// which let you omit the return keyword and braces if you can evaluate
// to something in one line
int MyFunc(int pParam) =>
   pParam;

Think of a lambda expression as saying, "given something, return something". In the example above, the lambda expression x => x says "given x, return x", although lambda expressions don't necessarily need to return something, in which case you might read them as "given x, do something with x".

Also note that there are kind of three things called "delegate" which can be very confusing at first.

An anonymous method uses the delegate keyword, but defines a method with no name:

Func<int, int> = delegate (int x) { return x; };

Assigning a method (anonymous, explicit, or lambda) to a reference causes a hidden Delegate wrapper object to be created that is what allows the method to be referred to. (Basically a sort of "managed function pointer".)

And then, you can also declare named method signatures using the delegate keyword as well:

public delegate int TestFunc(int x, int y);

TestFunc myFunc = delegate (int x, int y) { return x + y; };

This declares a named signature TestFunc that takes two ints and returns an int, and then declares a delegate reference of that type which is then assigned an anonymous method with matching signature.

Solution 3 - C#

It means awesomeness. For e.g.

x => x + 1

represents a method which takes x as a parameter and returns the successor of it.

button.Click += new EventHandler((sender, e) => methodInfo.Invoke(null, new object[] { sender, e }));

assigns an event handler to a button by invoking a method that a MethodInfo holds.

Solution 4 - C#

here's a simple example from msdn

delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25

Anything before the => are the input parameters, and anything after is the expression. You can have multiple input parameters. Lambdas are mainly used with Linq.

Solution 5 - C#

Instead of using anonymous method like this:

somevar.Find(delegate(int n)
{
   if(n < 10)
      return n;
});

you simply write it like this:

somevar.Find(n => n < 10);

It will take the data type based on the return value.

Solution 6 - C#

It basically means "goes into", like a parameter

MyObjectReference => MyObjectReference.DoSomething()

Usually you use them to pass functions into methods as parameters, or in LINQ statements

MyCollection.Where(myobj => myobj.Age>10)

For example.

Solution 7 - C#

It is part of the syntax of a lambda expression. A lambda expression is essentially a shortened form of a delegate or of an anonymous method. To illustrate, assume that I have an array of strings matching the letters of the alphabet. I could pick out the members of that array that contained values greater than "E" with the following LINQ expression:

var someLetters = alphabet.Where(l => l > "E");

The part of the lambda expression to the left of the "=>" identifies the variable name for the test (which is set to the individual members of alphabet) and the part of the lambda expression to the right of the "=>" identifies the processing. In this case the processing produces a boolean value that the Where logic uses to determine if each member of the alphabet is passed through to the someLetters array.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWolf5View Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Dave CousineauView Answer on Stackoverflow
Solution 3 - C#Serhat OzgelView Answer on Stackoverflow
Solution 4 - C#SteveView Answer on Stackoverflow
Solution 5 - C#milotView Answer on Stackoverflow
Solution 6 - C#Chris JamesView Answer on Stackoverflow
Solution 7 - C#JonStonecashView Answer on Stackoverflow