c# Func Action的使用

  1. 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();
    *

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值