delegate and lambda expression in c# code example

Example 1: c# lambdas

//Lambdas are just short handed anonymous method or functions usally 
//for use in passing to other methods or functions to call such as a sort.
//Example:
//The Lambda used here after the =s
Action ExampleContainerForDelagate = ()=>
	{/*Do something such as this example*/
    	Console.WriteLine("Hello world");
	};
//has been shorted from this anonymous delegate or method.
Action ExampleContainerForDelagate = delegate () { Console.WriteLine("Hello world"); };
//And this full (named) method signature method passing
Action ExampleContainerForDelagate = ExampleMethod;
void ExampleMethod()
{ Console.WriteLine("Hello world"); }
//usage example
List<int> ExampleList = new List(){1,3,0};
ExampleList.Sort((x,y)=>{return x - y; });
//The Sort will sort based on the return values from the Lambda passed.
//a negative will be seen as a smaller than the next number
//a 0 will be seen as equal
//and a positive will be seen as bigger than the next number
//by switching x and y postion in the 'return x - y;' you will effectively 
//change the sort to descending order. This is there for useful for sorting 
//based on needs or where it is not obvious how to sort.

Example 2: c# delegate lambda

// ------------ DELEGATE, ANONYMOUS FUNCTIONS and LAMBDA ------------ //

// DELEGATE:

public class Program
{
    // Delegate
    public delegate double MathOperation(double x, double y);
  
	// 1º Method
    static double Addition(double x, double y)
    {
        return x + y;
    }
  
	// 2º Method 
    static double Multiply(double x, double y)
    {
        return x * y;
    }

  	// 3º Method 
    static double Subtration(double x, double y)
    {
        return x - y;
    }

    // Main
    static void Main(string[] args)
    {	
      
      	// Normal Delegate:
      
        MathOperation myFunction = Addition;  

        Console.WriteLine("The math gives: " + myFunction(2.6, 8.0));
      
        MathOperation myFunction2 = Multiply;  

        Console.WriteLine("The math gives: " + myFunction2(7.9, 1.6));
      
      
        // Multicast Delegate:
      	
      	MathOperation multiFunction = Addition + Multiply;
      	multiFunction += Subtration;    // Add one more pointer to the method Subtration 
      
      	multiFunction(3.1, 4.5)  // It will call all 3 Methods with the same pair of parameters  
      
      	multiFunction -= Addition;   // Eliminate the pointer to the method Addition
      
        multiFunction(3.1, 4.5)  // It will call the Methods Multiply and Subtraction 
      
    }
}


// ANONYMOUS FUNCTIONS:

public class Program
{
    // Delegate
    public delegate double MathOperation(double x, double y);

    // Main
    static void Main(string[] args)
    {	
        // Anonymous function:
        MathOperation myFunction = delegate(double x, double y) { return x + y; };
      
        // You can also pass an anonymous function to methods:
      	Display(myFunction);
    }
  
  	static void Display(MathOperation myFoo)
    {
    	Console.WriteLine("The math gives: " + myFoo(2.8, 1.5));
      
    }

}
 

// LAMBDA:

public class Program
{
    // Delegate
    public delegate double MathOperation(double x, double y);

    // Main
    static void Main(string[] args)
    {	
        // Lambda function:
        MathOperation myFunction = (double x, double y) => { return x + y; };
        // OR
      	MathOperation myFunction2 = (x,y) => x * y;
    }
}