鸟语天空
委托与匿名方法
post by:追风剑情 2019-7-18 16:14

示例

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    delegate void Printer(string s);

    class Program
    {
        static void Main(string[] args)
        {
            // 使委托与匿名方法关联
            Printer p = delegate (string j)
            {
                System.Console.WriteLine(j);
            };
            p("The delegate using the anonymous method is called.");

            // 使委托与命名方法 (DoWork) 关联
            p = new Printer(DoWork);
            p("The delegate using the named method is called.");

            Console.ReadLine();
        }

        static void DoWork(string k)
        {
            System.Console.WriteLine(k);
        }
    }
}

运行测试

1111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容