using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// 示例1,避免大量的if else (switch) 语句,并提供更好的可扩展性
/// </summary>
namespace DelegateTest1
{
public delegate void GreetingDelegate(string name);
class Program
{
static void Main(string[] args)
{
MakeGreeting("汤姆",ChineseGreeting);
MakeGreeting("Tom",EnglishGreeting);
Console.ReadKey();
}
public static void MakeGreeting(string name, GreetingDelegate makeGgreeting)
{
makeGgreeting(name);
}
public static void EnglishGreeting(string name)
{
Console.WriteLine("Greetings " + name);
}
public static void ChineseGreeting(string name)
{
Console.WriteLine("你好 " + name);
}
}
}