首页 > Windows开发 > 详细

[C#] 委托与匿名方法

时间:2020-10-29 21:59:34      阅读:32      评论:0      收藏:0      [点我收藏+]
using System;
 
namespace 匿名函数
{
    class Program
    {
        delegate void TestDelegate(string s);
 
        static void M(string s)
        {
            Console.WriteLine("A参数为:{0}", s);
        }
 
        static void Main(string[] args)
        {
            //1. 委托的基本写法,及调用
            TestDelegate testDeleA = new TestDelegate(M);//通过委托的实例化将之与方法绑定
            testDeleA("hahahah");
 
            //2. 使用匿名表达式构造委托调用方法
            //C#2.0 Anonymous Method,其结构为:delegate+(各个参数)+{函数体}
            TestDelegate testDeleB = delegate (string s) { Console.WriteLine("B参数为:{0}", s); };//直接通过delegate声明将之与函数体绑定
            testDeleB("hehehehe");
            //C#3.0, 引入lamda表达法,Lambda Expression
            TestDelegate testDeleC = (x) => { Console.WriteLine("C参数为:{0}", x); };
            testDeleC("hohoho");
 
            Console.ReadKey();
        }
 
    }
}
 
 

[C#] 委托与匿名方法

原文:https://www.cnblogs.com/xihong2014/p/13898746.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!