Hi

Lambda expressions in c#

Lambda expressions are how anonymous functions are created.

Lambda expressions are anonymous functions that contain expressions or sequence of operators. 
All lambda expressions use the lambda operator =>, that can be read as “goes to” or “becomes”. 

The left side of the lambda operator specifies the input parameters and the right side holds an expression or a code block that works with the entry parameters. 
Usually lambda expressions are used as predicates or instead of delegates (a type that references a method).


Sample Code:


using System;  
using System.Collections.Generic;  
using System.Linq;  
public static class demo  
{  
    public static void Main()  
    {  
        List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };  
        List<int> evenNumbers = list.FindAll(x => (x % 2) == 0);  
  
        foreach (var num in evenNumbers)  
        {  
            Console.Write("{0} ", num);  
        }  
        Console.WriteLine();  
        Console.Read();  
  
    }  
}  

Output
2 4 6
Previous
Next Post »