=> Operator (C# Reference)

The => token is called the lambda operator. It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side. Lambda expressions are inline expressions similar to anonymous methods but more flexible; they are used extensively in LINQ queries that are expressed in method syntax. For more information, see Lambda Expressions (C# Programming Guide).

The following example shows two ways to find and display the length of the shortest string in an array of strings. The first part of the example applies a lambda expression (w => w.Length) to each element of the words array and then uses the Min method to find the smallest length. For comparison, the second part of the example shows a longer solution that uses query syntax to do the same thing.

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

// Use method syntax to apply a lambda expression to each element
// of the words array. 
int shortestWordLength = words.Min(w => w.Length);
Console.WriteLine(shortestWordLength);

// Compare the following code that uses query syntax.
// Get the lengths of each word in the words array.
var query = from w in words
            select w.Length;
// Apply the Min method to execute the query and get the shortest length.
int shortestWordLength2 = query.Min();
Console.WriteLine(shortestWordLength2);

// Output: 
// 5
// 5

Remarks

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

You can specify the type of the input variable explicitly or let the compiler infer it; in either case, the variable is strongly typed at compile time. When you specify a type, you must enclose the type name and the variable name in parentheses, as the following example shows.

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

Example

The following example shows how to write a lambda expression for the overload of the standard query operator Enumerable.Where that takes two arguments. Because the lambda expression has more than one parameter, the parameters must be enclosed in parentheses. The second parameter, index, represents the index of the current element in the collection. The Where expression returns all the strings whose lengths are less than their index positions in the array.

static void Main(string[] args)
{
    string[] digits = { "zero", "one", "two", "three", "four", "five", 
            "six", "seven", "eight", "nine" };

    Console.WriteLine("Example that uses a lambda expression:");
    var shortDigits = digits.Where((digit, index) => digit.Length < index);
    foreach (var sD in shortDigits)
    {
        Console.WriteLine(sD);
    }

    // Compare the following code, which arrives at the same list of short
    // digits but takes more work to get there.
    Console.WriteLine("\nExample that uses a for loop:");
    List<string> shortDigits2 = new List<string>();
    for (var i = 0; i < digits.Length; i++)
    {
        if (digits[i].Length < i)
            shortDigits2.Add(digits[i]);
    }

    foreach (var d in shortDigits2)
    {
        Console.WriteLine(d);
    }
    // Output:
    // Example that uses a lambda expression:
    // five
    // six
    // seven
    // eight
    // nine

    // Example that uses a for loop:
    // five
    // six
    // seven
    // eight
    // nine
}

See Also

Reference

Lambda Expressions (C# Programming Guide)

Concepts

C# Programming Guide

Other Resources

C# Reference