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!

About blogger..

ANIL SINGH

Hey! I am Anil Singh. I am Active Blogger, Writer and Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

Post a Comment
www.code-sample.com/. Powered by Blogger.