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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book