Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _05.委托_多點傳播委托
{
/// <summary>
/// 多點傳播委托,就是一次委托按照順序執行多個方法。
/// Note:如果使用多點傳播委托,該委托的簽名必須傳回void
/// </summary>
class Program
{
static void Main(string[] args)
{
//使用Action<T>實作多點傳播委托
Action<double> ops = MathOperations.MultiplyByTwo;
ops += MathOperations.Square;
ProcessAndDisplayNumber(3, ops);
Console.ReadKey();
}
static void ProcessAndDisplayNumber(double value, Action<double> ops)
{
ops(value);
}
}
class MathOperations
{
public static void MultiplyByTwo(double value)
{
Console.WriteLine("MultiplyByTwo={0}", value * 2);
}
public static void Square(double value)
{
Console.WriteLine("Square={0}", value * value);
}
}
}
源碼位址:點選打開連結