委托与匿名方法

作者:追风剑情 发布于:2019-7-18 16:14 分类:C#

示例

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

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号