委托,匿名函数
2022-07-17 本文已影响0人
山猪打不过家猪
委托
- 类似于python反射,将函数名当参数使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace delegate_test
{
class Program
{
public delegate void DeleSayHi(string str_name);
static void Main(string[] args)
{
SayEnglish("张三");
Test("彭菊菊", SayChinese);
Test("fxx", SayEnglish);
Console.ReadKey();
}
public static void Test(string str_name, DeleSayHi del)
{
del(str_name);
}
public static void SayChinese(string str_name)
{
Console.WriteLine($"我是{str_name}");
}
public static void SayEnglish(string str_name)
{
Console.WriteLine($"My name is {str_name}");
}
}
}