delegate in c# with example

Mapped Delegates static and instance methods

This is example is used for mapped both the static and instance methods in the Delegates using ASP.Net C#.

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

namespace Delegates
{
    delegate void MyDelegate();

    public class MyClass
    {
        public void InstanceMethods()
        {
            Console.WriteLine("Instance method call");
        }

        static public void StaticMethods()
        {
            Console.WriteLine("Static method call");
        }
    }

    public class MyMainClass
    {
        static public void Main()
        {
            MyClass MCObj = new MyClass();

            // Delegate Map to the instance method
            MyDelegate myDelInstObj = new MyDelegate(MCObj.InstanceMethods);
            myDelInstObj();

            // Delegate Map to the static method:
            myDelInstObj = new MyDelegate(MyClass.StaticMethods);
            myDelInstObj();
            Console.Read();
        }
    }

}

I hope it is helpful to you! Thank you!

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^