C# Action 委托

来源:互联网 发布:怎么关掉淘宝店铺 编辑:程序博客网 时间:2024/06/06 12:50

C# Action 委托

1.显示的 delegate

首先我们先看一个没有用Action的委托
using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;delegate void ShowWindowsMessageDelegate(string message);namespace testDelegate{    class Program    {        public static void Main()        {            ShowWindowsMessageDelegate showWindowsMessageDelegate;            showWindowsMessageDelegate = OnShow;            showWindowsMessageDelegate("Show Windows Message Success!");        }        private static void OnShow(string message)        {            MessageBox.Show(message);        }    }}
代码 很简单, 定义了一个显示窗口的代理, 用于显示一个窗口和一段文字

2.Action<in T> 委托

using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace testDelegate{    class Program    {        public static void Main()        {                       Action<string> ShowWindowsMessage;            ShowWindowsMessage = OnShow;            ShowWindowsMessage("Show Windows Message Success!");        }        private static void OnShow(string message)        {            MessageBox.Show(message);        }    }}
先看下Action<T>的描述

哦~~~ 原来Action<T>就是一个参数为T 的无返回值泛型委托

3.Action<in T1,in T2> 委托

那么两个参数的就自然想到了 Action<T1,T2> 
using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;public class TestAction1{    public static void Main()    {        Action<string, int> LogMessageByTimes;        LogMessageByTimes = OnLogByTimes;        OnLogByTimes("Log By Times Success!", 3);    }    private static void OnLogByTimes(string message, int times)    {        for (int i = 0; i < times; i++)        {            Console.WriteLine(message);        }    }}
运行结果


4.ForEach 委托

using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;class Program{    static void Main()    {        List<String> names = new List<String>();        names.Add("ForEach");        names.Add("is");        names.Add("Action<T> action");        names.Add("Actually");        // 参看ForEach 的描述 public void ForEach(Action<T> action);        // 参数 action 要对 System.Collections.Generic.List<T> 的每个元素执行的 System.Action<T> 委托。        // 所以 names.ForEach(Print); 这句代码其实也是使用的 Action<T> delegate        names.ForEach(Print);    }    private static void Print(string s)    {        Console.WriteLine(s);    }}


5.匿名方法作为委托参数

using System;using System.Windows.Forms;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;public class TestAnonMethod{    public static void Main()    {        Action<string> ShowWindowsMessage;        ShowWindowsMessage = delegate(string s) { OnShow(s); };        ShowWindowsMessage("Show Windows Message Success!");    }    private static void OnShow(string message)    {        MessageBox.Show(message);    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 中风后说话不清怎么办 老人吃不进去饭怎么办 老人吃什么就吐怎么办 老人吃了就吐怎么办 老人吃饭噎着了怎么办 胃胀气嗝不出来怎么办 嗓子咽口水都疼怎么办 产后盆底肌肉松弛怎么办 2个月宝宝鼻塞怎么办 人淹死捞不上来怎么办 胶囊卡在胃里怎么办 药卡在气管里怎么办 胶囊药卡在气管怎么办 被胶囊卡在喉咙怎么办 药卡在食道里怎么办 胶囊黏在喉咙里怎么办 要一直卡在喉咙怎么办 胃老是往上反气怎么办 有口气憋在喉咙怎么办 肛裂伤口不愈合怎么办 肛裂口子不愈合怎么办 宝宝胃食道反流怎么办 去角质后脸发红怎么办 红烧肉做的太甜怎么办 红烧排骨太甜了怎么办 唱歌时嗓子有痰怎么办 一唱歌喉咙有痰怎么办 鼻子老是打喷嚏还流鼻涕怎么办 鼻涕流到喉咙里怎么办 鼻塞怎么办怎样让鼻通气 流清鼻涕嗓子疼怎么办 喉咙疼咳嗽有痰怎么办 扁桃体发炎痛得厉害怎么办 腭垂掉下来了怎么办 喉咙干有异物感怎么办 嗓子干有异物感怎么办 输液的时候手疼怎么办 一感冒就嗓子哑怎么办 4岁儿童喉咙沙哑怎么办 嗓子老有异物感怎么办 喉咙咽口水都疼怎么办?