using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _000控制台練習
{
//聲明委托,寫在類外面,命名空間內
public delegate void delSay(string name);
class Program
{
static void Main(string[] args)
{
//調用這個函數,參數為“名字”和“委托”,就相當於“委托”來操作“名字”
//委托的實質就是,把函數當作函數的參數
test("jack", sayChinese);
//也可以把兩種打招呼方式賦值給一個“委托”,如果用+=,就是再綁定一個方法
//將輸出兩種打招呼方法
delSay delsay1 = sayEnglish;
delsay1 += sayChinese;
//或者也可以直接用“委托”操作“姓名”
delsay1("Ashley");
Console.ReadKey();
}
//寫一個函數,參數是“名字”和“委托”
public static void test(string name, delSay delsay)
{
//函數裡面是“委托”操作“名字”
delsay(name);
}
//打招呼,使用委托後就用不到了
public static void sayHello(string name)
{
sayEnglish(name);
}
//英語打招呼
public static void sayEnglish(string name)
{
Console.WriteLine("Hello," + name);
}
//漢語打招呼
public static void sayChinese(string name)
{
Console.WriteLine("你好," + name);
}
}
}