Action 和 Func 是 C# 内置的委托实例,它们都有很多重载以方便使用。
class Program
{
static void Main(string[] args)
{
Calculator calculator = new Calculator();
Action action = new Action(calculator.Report);
calculator.Report();//直接调用Report方法
action.Invoke();//间接调用Report方法
action();//简便写法
Func<int, int, int> func1 = new Func<int, int, int>(calculator.Add);
Func<int, int, int> func2 = new Func<int, int, int>(calculator.Sub);
int x = 100;
int y = 200;
int z = 0;
//z = func1.Invoke(x, y);
z = func1(x, y);
Console.WriteLine(z);
//z = func2.Invoke(x, y);
z = func2(x, y);
Console.WriteLine(z);
}
}
class Calculator
{
public void Report()
{
Console.WriteLine("I have 3 methods.");
}
public int Add(int a,int b)
{
int result = a + b;
return result;
}
public int Sub(int a, int b)
{
int result = a - b;
return result;
}
}