C# Program Example to Show Delegates Use

Here are 3 steps to use delegates in c sharp:
1. Declare Delegate
2. Instantiate delegate
3. Use Delegate
Here is the program showing these 3 things……
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
    public delegate void number();
    class Program
    {
        public static void myfun()
        {
            Console.WriteLine(“Delegate one is calling……”);
        }
        public static void myfun1()
        {
            Console.WriteLine(“Delegate one is callin…..”);
        }
        public static void myfun2()
        {
            Console.WriteLine(“Delegate 2 is calling……..”);
        }
        static void Main(string[] args)
        {
            number add = new number(myfun);
            add -= new number(myfun1);
            add += new number(myfun2);
            add();
            Console.ReadLine();
        }
    }
}

Add Comment