using System;
namespace LinQAndDeleGate
{
class Program
{
static void Main(string[] args)
{
MyDel del1 = new MyDel(M1);
del1 += M1;
del1.Invoke();
Action action = new Action(M1); //.net 4.0自带无返回值委托实例
action();
var action2 = new Action<string>(SayHi);//无返回值但是需要一个参数的委托实例(自带) var
action2("ZhangSan");
MyDelee<int> myadd = new MyDelee<int>(Add);//泛型委托实例
var res = myadd(100, 200);
Console.WriteLine(res);
var func = new Func<double, double, double>(Mul);
var res1 = func(3, 5);
Console.WriteLine(res1);
}
static void M1()
{
Console.WriteLine("M1 is Here!");
}
static void SayHi(string name)
{
Console.WriteLine($"{name},Hi!");
}
static double Mul(double a,double b)
{
return a * b;
}
static int Add(int a,int b)
{
return a + b;
}
}
delegate void MyDel();
delegate T MyDelee<T>(T a, T b);//泛型委托,自.net4.0开始,无返回值的委托可直接用Action,有返回值直接用Func
}
最后修改:2022 年 12 月 05 日
© 允许规范转载