C#委托代码记录
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
namespace 委托
{
public delegate void DelSayHollo(string name);
//DelSayHollo 是一个委托类型,表示可以指向任何接受一个 string 参数且返回 void 的方法
internal class Program
{
static void Main(string[] args)
{
//DelSayHollo del=new DelSayHollo (ChineseSayHollo);
DelSayHollo del = ChineseSayHollo; // 实例化委托
del("张三"); // 调用委托
Test("李四", EnglishSayHollo); //方法作为参数传给委托// 将EnglishSayHollo 方法作为参数传递给 Test 方法
Console.ReadKey();
}
public static void Test(string name,DelSayHollo del)// 定义一个方法,接受 DelSayHollo 委托作为参数
{
del(name);
}
public static void ChineseSayHollo(string name)// 定义一个方法,符合 DelSayHollo 委托的签名
{
Console.WriteLine("你好:"+name);
}
public static void EnglishSayHollo(string name) // 定义一个方法,符合 DelSayHollo 委托的签名
{
Console.WriteLine("Ness to meet you" + name);
}
}
}