Monday, 5 March 2018

015 C# Delegates

Delegates  delegates is an object , which is reference to a function or method. In.Net framework delegates is a special object which is used to call one on multiple function or method . Delegates is a concept of object oriented programming, it is type safe, similar to the C ,C++ plus function pointer.

                                    Delegates is reference type object, which object reference can be assign during run time or compile time. If you reference to a method to a delegate during compile time, it is also possible to change the reference during run time. Here is the example of delegate syntax.

 <Attribute><Modified> delegate return Type Name (Parameter List)

The access modifier define the accessibility of a delegates. Delegates also used to call back function and passing argument list to the method or function. Here is the example of declaration.

class A
{

public delegate int StudentCall();

}

You need to create instance of a delegate before use. Here is the example of create instance of a delegates. Creating instance of delegates new keyword is used. It is similar instance creation like class. System.Delegate  namespace is used for delegates.

 
class A
{

    public delegate int StudentCall();
    StudentCall obj = new StudentCall();
}


Below is a example of use of delegate without parameter and with parameter.

Example 1:  delegate without parameter

class School
{
     // declare delegate
     public delegate void StudentCall();

    static void Main(string[] args)
    {
       // StudentCall delegate points to GetFees
        StudentCall obj = GetFees;

        GetFees();
       // StudentCall delegate points to GetFees
        obj = GetAddress;

        GetAddress();
    }

     public static void GetFees()
    {
      //Console.WriteLine("Fees:"+'Roll);
    }

     public static void GetAddress()
    {
      // Console.WriteLine("RegistrationNumber:"+RegistrationNumber);
    }
}

Example 2:  delegate with parameter

 class School
{
          // declare delegate
          public delegate void StudentCall(int value);

         static void Main(string[] args)
         {
              // StudentCall delegate points to GetFees
                 StudentCall obj = GetFees;

                GetFees(10);
                GetFees(20);

            // StudentCall delegate points to GetFees
              obj = GetAddress;

              GetAddress(10003);
              GetAddress(200009);
        }

        public static void GetFees(int Roll)
       {
        //Console.WriteLine("Fees:"+'Roll);
       }

       public static void GetAddress(int RegistrationNumber)
       {
         // Console.WriteLine("RegistrationNumber:"+RegistrationNumber);
       }
}


There are three type of delegates 
  • Single Cast 
  • Multicast 
  • Generic  


Single Cast : Single cast delegates comes under the namespace System.Deligates. Single cast delegate point to a single method of function.

Example 1
 
class School
{
     // declare delegate
     public delegate void StudentCall();

    static void Main(string[] args)
    {
       // StudentCall delegate points to GetFees
        StudentCall obj = GetFees;

        GetFees();
       // StudentCall delegate points to GetFees
        obj = GetAddress;

        GetAddress();
    }

     public static void GetFees()
    {
      //Console.WriteLine("Fees:"+'Roll);
    }

     public static void GetAddress()
    {
      // Console.WriteLine("RegistrationNumber:"+RegistrationNumber);
    }
 
Multicast : Multicast delegates comes under the namespace System.Multicast. Multicast delegates point to call method of function + operator and - operator. This operator assign and remove method pointer from delegates to do multi casting.

Example 1

class School
{
   // declare delegate
      public delegate void StudentCall();

       static void Main(string[] args)
      {
        StudentCall obj = new StudentCall(Example1);
        Example1();

        obj += new StudentCall(Example2);
        Example2();

         obj -= new StudentCall(Example2);

        obj += new StudentCall(Example3);
        Example3();

       obj -= new StudentCall(Example3);
       Example3();

     obj += new StudentCall(Example4);
      Example4();

     obj -= new StudentCall(Example4);
    Example4();

     obj += new StudentCall(Example5);
     Example5();
}

             public static void Example1()
            {
              Console.WriteLine("Example 1");
             }

           public static void Example2()
           {
            Console.WriteLine("Example 2");
           }

            public static void Example3()
           {
           Console.WriteLine("Example 3");
           }

           public static void Example4()
           {
            Console.WriteLine("Example 4");
          }

         public static void Example5()
         {
          Console.WriteLine("Example 5");
         }
}


Generic type : Generic type is a delegate call on argument . When you are trying to call any type want to return any type, generic delegate is used. Here is the example of genetic delicate.


class School
{
        public delegate T1 StudentCall(T1 a, T2 b);

        static void Main(string[] args)
        {
         
             StudentCall<int, int> gdInt = new StudentCall<int, int>(Example1);

         }

         public static int Example1(int a, int b)
         {
          return (a + b);
         }

        public static string Example2(string a, string b)
        {
         return (a + b);
        }

        public static int Example3(int a, int b)
        {
         return (a * b);
        }

       public static string Example3(string a, string b)
      {
        return (a + b);
       }

      public static string Example4(string a, int b)
      {
       return (b.ToString());
      }

}

Call The deligate:

School.StudentCall<int, int> obj = School.Example1;
Response.Write(School.Example1(5, 6));
//Output:11
Console.WriteLine(School.Example2("Hellow ", "World").ToString());
//Output:Hellow World
Console.WriteLine(School.Example3(5, 6));
//Output:30
Console.WriteLine(School.Example4("Hellow", 6));
//Output:6




Delegate have some other features. We already noticed, signature, parameter , return type should be same when we are invoking a delegates. There's some flexibility on this rule.. Net Framework allow some extent of flexibility of return type and parameter.


Covariance :Inheritance allowed if class to be derived from another class. A class, called base class, another class derived from this class create relationship between two class. Member variable and functionality of base class comes to derived class as per declaration. Covariance allow to pass derived class when Baset classes expected. A derived class have some more functionality and member variable then base class. Covariance allow a derived class pass in place of a base class. Covariance allow flexibility in return also. Here is the example of Covariance.

class A
{

}
class B : A
{

}
class C : B
{

}

class School
{
            public delegate A StudentCall(A a);

           static void Main(string[] args)
            {
              StudentCall handler = Example1;
              Console.WriteLine(handler.GetType().ToString());

              StudentCall handler2 = Example2;
             Console.WriteLine(handler2.GetType().ToString());
           }

           public static A Example1(A a)
          {
              return (a);
           }

          public static B Example2(A a)
          {
         return new B();
         }

        public static B Example2(C a)
        {
         return new C();
        }

}

Call The deligate:
School.StudentCall handler = School.Example1;
// Console.WriteLine(handler.GetType().ToString());
Response.Write(handler.GetType().ToString());
Response.Write("
"
);

School.StudentCall handler2 = School.Example2;
// Response.Write(handler2.GetType().ToString());
Console.WriteLine(handler2.GetType().ToString());



Contravariance : Contravariance allow us some flexibility in passing parameters.Derived class can be passed when  a base class is expected. Here is the example of Contravariance.


class A
{

}
class B : A
{

}
class C : B
{

}

class School
{
      public delegate A StudentCall(A a);

       static void Main(string[] args)
      {

           A a = new A();

         // Delegating Example1
         StudentCall del = Example1;
         del(a);

        // Delegating Example2
        StudentCall del2 = Example2;
        del(a);

     }
     public static A Example1(A a)
    {
    return (a);
    }

     public static B Example2(A a)
     {
      return new B();
     }
     public static B Example2(C a)
     {
     return new C();
    }
}

Event Handler

            Event Handler is a special object which response to an event. Each event  must be associate with  delegate . Event handler and associate delegate signature must be match. Event handler is parameter type. The first parameter is sender  object,sender who has fired the event is the object. The second parameter is data associated with the Handler. The data class must be inherited from the class EventArg .EventArg is the base class of even data type . 

Handler can be two type , Static or Dynamic. Static event Handler  is an object which  have you fixed object event to handle, through the entire life time. Dynamic event handler which  activate deactive, depending upon the programming.

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...