delegate

作者:追风剑情 发布于:2014-8-19 20:50 分类:C#


using System;

namespace DelegateTest
{
    public delegate string MyMethodDelegate(int i);

    class Program
    {
        static void Main(string[] args)
        {
            TestClass tc = new TestClass();
            MyMethodDelegate myDeleg1 = new MyMethodDelegate(tc.CallBack2);
            MyMethodDelegate myDeleg2 = new MyMethodDelegate(TestClass.CallBack1);
            CallDelegate(myDeleg1);
            CallDelegate(myDeleg2);
            CallDelegate(new MyMethodDelegate(delegate(int i){
                return "匿名函数-"+i;
            }));

            Console.ReadKey();
        }

        static void CallDelegate(Delegate deleg)
        {
            Console.WriteLine( deleg.DynamicInvoke(9) );
        }
    }

    class TestClass
    {
        public static string CallBack1(int i)
        {
            return "CallBack1-"+i;
        }

        public string CallBack2(int i)
        {
            return "CallBack2-"+i;
        }
    }
}


运行结果

调用委托方法运行结果.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号