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();
}
}
}
using System; namespace 匿名函数 { class Program { static void Main(string[] args) { //调用线程 StartThread(); Console.ReadKey(); } private static void StartThread() { //创建线程 System.Threading.Thread t1 = new System.Threading.Thread ( delegate ()//参数不能使用ref、out这种关键字修饰,以及匿名方法不能放在is关键字左边 { //函数体中不能有unsafe code Console.Write("Helo, "); Console.WriteLine("World!"); } ); //开启线程 t1.Start(); } } }
原文:https://www.cnblogs.com/ahoka/p/12373620.html