匿名函数委托有两个优点
1、在函数中定义函数
2、可以调用函数外的变量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 委托
{
delegate void AnimalPlay(string name);//委托 格式必须与要委托的函数相似
class Program
{
static void RunCircus(AnimalPlay animalPlay,string name)//函数做参数
{
animalPlay(name);
}
static void Main(string[] args)
{
string a = "!!!";
RunCircus(delegate(string name){ Console.WriteLine("哈哈哈,我是匿名函数" + name+a);}, "dawa");//执行函数
}
}
}
输出:
哈哈哈,我是匿名函数dawa!!!
在方法中定义了方法
也调用了外部的参数 a