Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does '=>' do in C#? [duplicate]

Tags:

operators

c#

Possible Duplicates:
Lamda Explanation and what it is as well as a good example
What is the => token called?

I have seen this code:

myContext.SomeEntities.Single(x => x.code == code);  

And I don´t know what does the => operator do.

Every search on google about the operator returns no results.

Thank you.

like image 629
Javiere Avatar asked Jun 21 '11 17:06

Javiere


4 Answers

The => operator designates a Lambda Expression:

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

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:

static void Main(string[] args)
{
    Func<int, int> func = x => x * x;
    int j = func(5);
    // j == 25
}
like image 137
dtb Avatar answered Oct 04 '22 21:10

dtb


Lambda expressions, very cool.

http://msdn.microsoft.com/en-us/library/bb397687.aspx

like image 36
Dean Thomas Avatar answered Oct 04 '22 21:10

Dean Thomas


This is defining a lambda. You can read it "x goes to x.code equals code," and it means that given x, return the result of the given comparison.

like image 43
David Ruttka Avatar answered Oct 04 '22 21:10

David Ruttka


It signals that the code is a lambda expression.

More info: http://msdn.microsoft.com/en-us/library/bb397687.aspx

like image 34
Jeff Avatar answered Oct 04 '22 23:10

Jeff