Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Delegate in C#:-


It is called function pointer means we can point the address of a function using a delegate to call that function.

If we do not want to call a particular function in the program then we can use the delegate concepts to call that function indirectly.

Type of Delegate:-

1) Single Cast  Delegate:-  We can call one delegate to one function

class DelegateExample
    {
        public void fun()
        {
            Console.WriteLine("Fun");
        }
        public delegate void mydelegate();
        static void Main()
        {
            DelegateExample del = new DelegateExample();
            mydelegate obj = new mydelegate(del.fun);
            obj();
            Console.ReadKey();
        }
    }
2) Multicast Delegate :-  We can call one delegate to multiple function

public delegate void delegatename(param);
class DelegateExample
    {
        public void fun()
        {
            Console.WriteLine("Fun");
        }
        public void fun1()
        {
            Console.WriteLine("Fun1");
        }
        public void fun2()
        {
            Console.WriteLine("Fun2");
        }
        public delegate void mydelegate();
        static void Main()
        {
            DelegateExample del = new DelegateExample();
            mydelegate obj = new mydelegate(del.fun);
            obj += del.fun1;
            obj += del.fun2;
            obj();
            Console.ReadKey();
        }
    }


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post