sunshine



Delegates in C#

What is a delegate:

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.(This is a Microsoft definition)

What does the above statement mean ?

The definition of a delegate given above can be a little confusing for a novice programmer. You know that you can pass parameters to a function. Usually the parameter is a type ( like string or an integer or a class you defined etc). Can a function itself be a parameter? ie A function takes another function as parameter. Look at the below code. A function MathCalculations calls the function Square. Square got a parameter named input. Nothing fancy.

MathCalculations()

{

  int y = Square(10);

}

int Square(int input) 

{ 

  return x*x;

}

Suppose you have a bunch of different calculations and not just square(For simplicity lets assume all of them have only one parameter integer). Instead of calling Square, can we call a generic calculation function and have it a parameter which would be the specific function. See the code below.

MathCalculations()

{

  int y = GenericCalculation(Square,10);

  int z = GenericCalculation(Cube,10);

  int z = GenericCalculation(SquareRoot,10);

}

int GenericCalculation( Calc Generic, int x )

{ 

  return Calc(x);

}

So GenericCalculation’s first parameter is another function which would do the specific calculation you want. The type delegate would help you to define a reference to a function and pass it like a variable. Following is the complete code. Try it out.

class Program

{

     public delegate int Calc(int x);  //This will hold the reference to a function

     static void Main(string[] args)

     {

            Program p = new Program();

            Calc generic = p.Square;//Assign the delegate with the reference of Square function

            int square = p.GenericCalculation(generic, 5);//Assign the delegate with the reference of Square function

            Console.WriteLine("Square of 5 is {0}", square);

            generic = p.Cube; //Assign the delegate with the reference of Cube function

            int cube = p.GenericCalculation(generic, 5); //Call the cube function via the delegate

            Console.WriteLine("Cube of 5 is {0}", cube);

            Console.ReadKey();



     }

     int GenericCalculation( Calc Generic, int x )

     {

            return Generic(x);

     }

     int Square(int x)

     {

            return x * x;

     }

     int Cube(int x)

     {

           return x * x * x;

     }

}

Great! You can pass a set of code ( function ) as a parameter, to other functions. What is the big deal?

Suppose your code is calling a third party call and you want to get status from that code? You can pass a function to the code and third party code can call back to your code which is called callbacks. This is typically handled by events which is based on delegates.

Another scenario is that you don’t know in advance what to call but know only at run time. You can wire it to a delegate and then later at run time provide the right code.

Anonymous functions and lambda expressions are a step ahead of delegates.

We offer one on one C# training,SQL training,WCF training,WPF training,WinForms training, MVC training, ASP.NET training, Ajax & Javascript training. Please see the contacts page