Saturday, February 8, 2014

_delegate_ in c#

_delegate_ is a reference type in c# which can be used to create a new delegate type. This new delegate type also acts as a reference type that can store a reference to an instance of a delegate Object. A delegate Object is a special kind of object which can hold references to certain methods and later be used to invoke those methods via the delegate Object. Delegates seem to work like Function pointers in C or C++, but they are totally different to Function pointers because they are object-oriented, type-safe, and secure.

Now we can see that there are three basic steps in using a delegate.

1.  Create a new delegate type using delegate keyword. => Declaring a delegate

2. Create a delegate object from new delegate type and populate the object with references to methods. => Instantiating a delegate

3. Use delegate object to invoke the methods encapsulate by the object. => Calling a delegate

Let's discuss these three steps in detail.

1. Declaring a delegate

Declare the delegate in a way that it resembles the signature of the method or methods you will later  associate with it. Following is the structure of declaring a delegate type.

delegate return_type new_type_name(argument list)

   Examples :-

   delegate void delageteType();

   delegate string delageteTypeA(int a, string b);


   delegate TestClass delageteTypeB(double a);


Also you can add access modifiers with delegates as any other class member.

   Ex:- public delegate void delageteType();

2.  Instantiating a delegate

This step includes two sub-steps namely,
   I. Create a delegate object of the newly declared delegate type while associating it with a particular method.
  II. This is optional. Associate the object with methods of same signature (i.e. multicast).

Assume we have functions as follows.

class Program{
   static void testFunctionA()
        {
            Console.WriteLine("functionTestA is invoked");
        }

   static void testFunctionB()

        {
            Console.WriteLine("functionTestB is invoked");
        }

   private void testFunctionC()

        {
            Console.WriteLine("functionTestC is invoked");
        }
}

Now we can instantiate our delegate following the aforementioned sub-steps.

Example for sub-step I,

   DelegateType DelegateObject = new DelegateType(testFunctionA);

Alternatively you can use the following syntax to do the above.
   
   DelegateType DelegateObject = testFunctionA;

Example for sub-step II,

   DelegateObject +=  testFunctionB;

Note that we can add any type of method with same signature to the delegate object. For example we can add an instance method with the same signature as follows.

  DelegateObject +=  new Program().testFunctionC;

Note that as we can add methods using += we can also remove existing methods from delegate object using -=.

   DelegateObject -=  testFunctionB;

3. Calling a delegate

Calling the delegate object is like calling a method.  It is called by the name of the delegate object, followed by parenthesized arguments.

Examples :-

   DelegateObject(); // delegate type declaration => delegate void delageteType();

   string result = DelegateObjectA(2, "test"); // delegate type declaration => delegate string delageteTypeA(int a, string b);

   TestClass resultObject = DelegateObjectB(1.204); // delegate type declaration => delegate TestClass delageteTypeB(double a);

Now let's sum up all the above code in a simple c# class called Program.cs.

class Program
    {
        public delegate void DelegateType(); //Step 1

        static void testFunctionA()
        {
            Console.WriteLine("functionTestA is invoked");
        }

        static void testFunctionB()
        {
            Console.WriteLine("functionTestB is invoked");
        }

        private void testFunctionC()
        {
            Console.WriteLine("functionTestC is invoked");
        }

        static void Main(string[] args)
        {
            DelegateType DelegateObject = testFunctionA; // Step 2 -I
            DelegateObject += testFunctionB; // Step 2 -II
            DelegateObject += new Program().testFunctionC; // Step 2 -II
            DelegateObject -= testFunctionB; // Step 2 -II

            DelegateObject(); // Step 3
            Console.ReadKey();
        }
    }








No comments:

Post a Comment