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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

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