How to call static method using delegate in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    public class MyClass
    {
        public delegate void mydelegate(string s);

        public void myclassmethod(mydelegate mydgt)
        {
            mydgt?.Invoke("myclassmethod");
        }
    }

    class Program
    {
        static void amethod(string s)
        {
            Console.WriteLine("method: " + s);
        }
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();

            MyClass.mydelegate mydgt = new MyClass.mydelegate(amethod);

            obj.myclassmethod(mydgt);
        }
    }
}

/*
run:
    
method: myclassmethod
       
*/

 



answered Apr 22, 2017 by avibootz
edited Apr 22, 2017 by avibootz

Related questions

1 answer 148 views
1 answer 146 views
1 answer 151 views
1 answer 197 views
1 answer 210 views
1 answer 161 views
...