Func和Action的用法,BeginInvoke和Invoke的使用

Func和Action的用法


个人学习总结:

Action< T >的用法 (同步)

class Program
    {
      //Action<T>的用法
        static void Main(string[] args)
        {
            Action<string> FoodAction = new Action<string>(Food);
            FoodAction("鱼香肉丝");
            Console.ReadKey();
        }
        public static void Food(string foodName)
        {
            Console.WriteLine("我出去吃了{0}",foodName);
        }
    };

Action< T1, T2>的用法

class Program
    {
      
        static void Main(string[] args)
        {
            //Action< T1, T2>
              Action<string,string> FoodAction = new Action<string,string>(Food);
              FoodAction("鱼香肉丝","张三餐馆");
            
            Console.WriteLine("王四也来吃饭了");
            Console.ReadKey();
        }
        public static void Food(string foodName,string restaurant)
        {
           // Thread.Sleep(5000);
            Console.WriteLine("张三出去吃了{0},在{1}吃的饭",foodName,restaurant);
        }
    }

Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线程上执行指定的委托。
Control.BeginInvoke 方法 (Delegate) :在创建控件的基础句柄所在线程上异步执行指定委托。

运行后生成顺序:

张三出去吃了鱼香肉丝,在张三餐馆吃的饭
王四也来吃饭了
 class Program
    {
      
        static void Main(string[] args)
        {
            //Action< T1, T2>
              Action<string,string> FoodAction = new Action<string,string>(Food);
              FoodAction.BeginInvoke("鱼香肉丝","张三餐馆",null,null);
            
            Console.WriteLine("王四也来吃饭了");
            Console.ReadKey();
        }
        public static void Food(string foodName,string restaurant)
        {
            Thread.Sleep(5000);
            Console.WriteLine("张三出去吃了{0},在{1}吃的饭",foodName,restaurant);
        }
    }

直接写在函数体内

static void Main(string[] args)
        {
            
            Action<string, string> FoodAction = (string a, string b) =>
              {
                  Thread.Sleep(5000);
                  Console.WriteLine("张三出去吃了{0},在{1}吃的饭", a, b);
              };
            FoodAction.BeginInvoke("鱼香肉丝", "张三餐馆", null, null);

            Console.WriteLine("王四也来吃饭了");
            Console.ReadKey();
        }
运行后生成顺序:

王四也来吃饭了
张三出去吃了鱼香肉丝,在张三餐馆吃的饭
 class Program
    {
        public string Name { get; set; }
        public string FoodName { get; set; }
        public string Restaurant { get; set; }

        static void Main(string[] args)
        { 
           
            Action<string, string> FoodAction = new Action<string, string>(Food);
            //第一个是AsyncCallback委托是异步完成的回调方法。第二个是用户自定义对象,该对象将传递到回调方法中。
            //BeginInvoke返回IAsyncResult接口,可用于检测异步调用的过程。

            AsyncCallback call = new AsyncCallback(Food2);
            Program pro = new Program();
            pro.FoodName = "红烧茄子";
            pro.Restaurant = "六麻子饭店";
            FoodAction.BeginInvoke("鱼香肉丝","张三餐馆" ,call, pro);
            Console.WriteLine("王四也来吃饭了");
            Console.ReadKey();
        }
        public static void Food(string foodName, string restaurant)
        {
            Thread.Sleep(1000);
            Console.WriteLine("张三出去吃了{0},在{1}吃的饭", foodName, restaurant);
        }
     
        public static void Food2(IAsyncResult ar)
        {
            Program pro = (Program)ar.AsyncState;
            //Thread.Sleep(1000);
            Console.WriteLine("六麻子出去吃了{0},在{1}吃的饭",pro.FoodName,pro.Restaurant);
        }
    }
运行后生成顺序:
王四也来吃饭了
张三出去吃了鱼香肉丝,在张三餐馆吃的饭
六麻子出去吃了红烧茄子,在六麻子饭店吃的饭

 static void Main(string[] args)
        {
            Func<string> FoodFunc = new Func<string>(Food);
           
            Console.WriteLine(FoodFunc());
            Console.WriteLine("赵四也来吃饭了");
            Console.ReadKey();
        }
        public static string Food()
        {
            Thread.Sleep(3000);
            return ("鱼香肉丝");
        }
运行后生成顺序:

鱼香肉丝
赵四也来吃饭了
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的ActionFunc是两种常用的委托类型。Action是一个没有返回值的委托,而Func是一个有返回值的委托。\[1\] 在代码示例中,使用FuncAction来定义委托并执行相应的方法。Func可以定义带有不同参数和返回值类型的委托,而Action只能定义带有参数但没有返回值的委托。\[1\] 在示例中,MyFun1是一个无参的Func委托,返回一个string值。MyFun2是一个带有一个string参数的Func委托,返回一个string值。MyFun3是一个带有两个string参数的Func委托,返回一个bool值。\[1\] 另外,示例中还展示了使用Action来定义委托的写法。在Start方法中,使用Action定义了一个带有一个string参数的委托act,并执行了Init方法。\[2\] 此外,还可以使用Lambda表达式来创建委托。Lambda表达式可以简化委托的定义和使用。在示例中,使用Lambda表达式创建了一个带有一个string参数的Action委托。\[3\] 总结来说,ActionFuncC#中常用的委托类型,用于定义不同参数和返回值类型的委托Action用于定义没有返回值的委托,而Func用于定义有返回值的委托。可以使用Lambda表达式来简化委托的定义和使用。 #### 引用[.reference_title] - *1* *2* [c#ActionFunc(简单用法)](https://blog.csdn.net/qq_39984000/article/details/115245134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C#语法:C# 简述Actionfunction内置委托用法、Lambda 表达式创建委托最为方便](https://blog.csdn.net/qq_37271216/article/details/102801792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值