C# 委托
Action和Func是.NET类库的内置委托,以简便使用。
Func有17个重载
还可以使用delegate关键字创建委托
下面的代码展示了这三种使用委托的方法
namespace _20200327
{
public delegate int Delga(int a, int b);
class Program
{
static void Main(string[] args)
{
Calculate cal = new Calculate();
Action action = new Action(cal.Report);
action();
Func<int, int, int> funcAdd = new Func<int, int, int>(cal.Add);
int a = 100;
int b = 200;
int c = 0;
c = funcAdd(a, b);
Console.WriteLine(c);
Delga delgaSub = new Delga(cal.Sub);
c = delgaSub(a, b);
Console.WriteLine(c);
Console.ReadKey();
}
}
class Calculate
{
public void Report()
{
Console.WriteLine("3个方法");
}
public int Add(int a, int b)
{
return a + b;
}
public int Sub(int a,int b)
{
return a - b;
}
}
}



鲁公网安备 37148202000241号