C#委托_lambda表达式
lambda表达式
格式:(参数)=>{要执行的代码}; 参数:相当于绑定方法的参数
{};里面相当于绑定方法的方法体
由于是赋值,是—个语句,后面的;不要省
示例
class Program//使用委托判断a是否大于b
{
public delegate bool MyDelegate(int a,int b);
static void Main(string[] args)
{
MyDelegate myDelegate = (int a, int b) =>
{
bool result = false;
if (a >= b)
{
result = true;
}
else
{
result = false;
}
return result;
};
bool temp= myDelegate(7,10);
Console.WriteLine(temp);
}
}
//输出结果为false