Delegates in C#

Delegates:

A delegate is a new type of object in C#.A delegate is a pointer to a method.Delegate is used to call a method by using memory address of the method. Any method that a delegate
instance refers to must conform to the signature of the delegate. After a method has been
assigned to a delegate, it is called when the delegate is invoked.

Single cast Delegates:

A single cast delegate is a delegate which can hold reference of only one method.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegates
{
    delegate void Doubleop();          //Delegate declaration
    class Program
    {
       

        static void Main(string[] args)
        {
          //Assinging method to delegate instance

          Doubleop op =new Doubleop(Person.Input);
          Doubleop op1= new Doubleop(Person.Output);
                   op();   //invoking delegate
                   op1();
        }
       
    }
    class Person
    {
        static string name;
        static string Per_no;
        static int age;
        public static void Input()
        {
            Console.Write("Enter person_no ");
            Per_no = Console.ReadLine();
            Console.Write("Enter name ");
            name = Console.ReadLine();
            Console.Write("Enter age ");
            age = int.Parse(Console.ReadLine());

        }
        public static void Output()
        {
         
            Console.WriteLine("Name is {0} ", name);
          
            Console.WriteLine("Emp_no is {0} ", Per_no);

            Console.WriteLine("Age is {0} ", age);
        }
    }
}



Multicast Delegates:
Multicast delegate can hold reference of more than one methods when delegate will be invoked all the methods assigned to that delegate will be executed.
Note:
Return type of multicast delegates should be void.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegates
{
    delegate void Doubleop();          //Delegate declaration
    class Program
    {
       

        static void Main(string[] args)
        {
          //Assinging multiple methods to single delegate instance

           Doubleop multicast = new Doubleop(Employee.Input);
 multicast += new Doubleop(Employee.Output);
 multicast();   //invoking delegate
}
       
    }

class Employee
    {
        static string name;
        static string Emp_no;
        static int age;
        public static void Input()
        {
          

            Console.Write("Enter Emp_no ");
            Emp_no = Console.ReadLine();
            Console.Write("Enter name ");
            name = Console.ReadLine();
            Console.Write("Enter age ");
            age = int.Parse(Console.ReadLine());

        }
        public static void Output()
        {
            Console.WriteLine("Name is {0} ", name);
            Console.WriteLine("Emp_no is {0} ", Emp_no);
            Console.WriteLine("Age is {0} ", age);
        }
    }
}

Comments

Popular posts from this blog

Using Progress Bar In C#

Get elements by class name in javascript

Jquery serer side datatables in asp.net