-
c# Func Action的使用
Action :
1. 新建一个names方法 有参数 name
public static void studio(string age)
{
Console.WriteLine($“我是学生 今年{age}岁”);
}
使用Action 对此方法进行调用
Action act =new Action(studio);
act(“18”);无参数调用
public static void teacher() { Console.WriteLine("我是老师"); } Action acts = teacher; acts(); 有返回值 有参数
public static int studio(int age)
{
return age;
}
Func<int, int> act =new Func<int,int>(studio);
act(18);总结: Func <参数1,参数2> 参数一 :表示 需要传参数类型 参数二 :表示 返回值类型 没有可不写
有返回值用Func 无返回值用Action
使用func action 封装方法
public static string studio(int age) { return "我是学生我今年"+age+"岁了"; } public static string teacher(int age) { return "我是老师我今年" + age + "岁了"; } public static void list(Func<int, string> func,int age) { Console.WriteLine(func(age)); }
调用 Console.WriteLine(“学生请输入1,老师请输入2”);
String type=Console.ReadLine();
Console.WriteLine(“输入你的年龄”);
int age = int.Parse(Console.ReadLine());
if (type == “1”)
{
Func<int, String> func = new Func<int, string>(studio);
list(func,age);
}
else
{
Func<int, String> func = new Func<int, string>(teacher);
list(func, age);
}
Console.ReadLine();
*
c# Func Action的使用
最新推荐文章于 2020-06-01 10:03:41 发布