Introduction edit

Lambda expressions are one of the forms of anonymous methods in .Net, which can be used for passing as inputs for local functions or as an expression to execute inline. Lambda expressions are particularly helpful for writing LINQ query expressions.

Lambda expressions helps in simplifying the logical statements and looping statements. This is possible for enumerable types where we can use Lambda expressions instead of writing looping statements or logical statements in multiple execution lines making code look cleaner and simple.

Example edit

using System;
using System.Linq;

namespace Program
{
  class Program
  {
    public static void Main(string [] args) 
    {
      int[] myInteger = new int[] {1,2,3,4,5}; //Creates an integer array named myInteger and initializes it with 5 values(1, 2, 3, 4, 5). 1 is in index 0, 2 is in index 1, etc
      var getNumbers = from num in myInteger  where num > 1 select num;//similar to a SQL query. Return a collection of all numbers greater than one
     
       foreach(int number in getNumbers)//loop through that collection we just made.
       {
          Console.WriteLine(number);//2, 3, 4, 5
       }
       Console.ReadKey();
     }
  }
}