⇒ Lambda operator

suggest change

The => operator has the same precedence as the assignment operator = and is right-associative.

It is used to declare lambda expressions and also it is widely used with LINQ Queries:

string[] words = { "cherry", "apple", "blueberry" };

int shortestWordLength = words.Min((string w) => w.Length); //5

When used in LINQ extensions or queries the type of the objects can usually be skipped as it is inferred by the compiler:

int shortestWordLength = words.Min(w => w.Length); //also compiles with the same result

The general form of lambda operator is the following:

(input parameters) => expression

The parameters of the lambda expression are specified before => operator, and the actual expression/statement/block to be executed is to the right of the operator:

// expression
(int x, string s) => s.Length > x // expression
(int x, int y) => x + y // statement
(string x) => Console.WriteLine(x)
// block
(string x) => {
        x += " says Hello!";
        Console.WriteLine(x);
    }

This operator can be used to easily define delegates, without writing an explicit method:

delegate void TestDelegate(string s);

TestDelegate myDelegate = s => Console.WriteLine(s + " World");

myDelegate("Hello");

instead of

void MyMethod(string s)
{
    Console.WriteLine(s + " World");
}

delegate void TestDelegate(string s);

TestDelegate myDelegate = MyMethod;

myDelegate("Hello");

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Operators:
* Syntax
* sizeof
* Lambda operator
* typeof

Table Of Contents
2 Operators
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script