国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Func<T>與Action<T>委托泛型介紹

2019-11-08 02:06:59
字體:
來源:轉載
供稿:網友

  .Net 3.5之后,微軟推出了Func<T>與Action<T>泛型委托。進一步簡化了委托的定義。 

  Action<T>委托主要的表現形式如下:

        public delegate void Action();        public delegate void Action<T1>(T1 arg1);        public delegate void Action<T1, T2>(T1 arg1, T2 arg2);        public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);        public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);        public delegate void Action<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);

  從Action<T>的定義形式上可以看到。Action<T>是沒有返回值得。適用于任何沒有返回值得方法。例如:

復制代碼
/// <summary>        /// 應用程序的主入口點。        /// </summary>        [STAThread]        static void Main()        {            //同步執行            Action Action = new Action(writeLine);            Action.Invoke();            //異步執行            Action ActionAsy = new Action(writeLine2);            ActionAsy.BeginInvoke(resual=>Console.WriteLine("異步執行結束"), null);            Console.Read();        }        PRivate static void writeLine()        {            Console.WriteLine("Action同步執行委托");        }        private static void writeLine2()        {            Console.WriteLine("Action異步執行委托");        }復制代碼

  如果調用Lambda表達式,可以更簡練,對上面的代碼,可以這樣寫:

復制代碼
/// <summary>        /// 應用程序的主入口點。        /// </summary>        [STAThread]        static void Main()        {            //同步執行 用Lambda表達式代替writeLine            Action Action = new Action(()=>Console.WriteLine("Action同步執行委托"));            Action.Invoke();            //異步執行 用Lambda表達式代替writeLine2            Action ActionAsy = new Action(()=>Console.WriteLine("Action異步執行委托"));            ActionAsy.BeginInvoke(resual=>Console.WriteLine("異步執行結束"), null);            Console.Read();        }        private static void writeLine()        {            Console.WriteLine("Action同步執行委托");        }        private static void writeLine2()        {            Console.WriteLine("Action異步執行委托");        }復制代碼

  如果有參數需要傳入,Action<T>可以這么做,例如:

復制代碼
/// <summary>        /// 應用程序的主入口點。        /// </summary>        [STAThread]        static void Main()        {            //同步執行 傳入一個參數            Action<string> Action = new Action<string>((a)=>Console.WriteLine(string.Format("Action同步執行委托,傳入參數:{0}",a)));            Action.Invoke("小李");            //異步執行 傳入兩個參數            Action<string,int> ActionAsy = new Action<string,int>((a,b)=>Console.WriteLine("Action異步執行委托,傳入參數:{0},{1}",a,b));            ActionAsy.BeginInvoke("小李",12,resual=>Console.WriteLine("異步執行結束"), null);            Console.Read();        }復制代碼

  在上面代碼中,同步定義的string類型,必須保證傳入的參數a也是string。雖然并沒有對a進行類型定義,但是系統默認就是事先泛型中定義的類型。類似的,異步委托也是一樣。不然會報錯。

   Func<T>委托主要的表現形式如下:

  

        public delegate TResult Func<TResult>();        public delegate TResult Func<T1, TResult>(T1 arg1);        public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);        public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);        public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);        public delegate TResult Func<T1, T2, T3, T4, T5, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);

  Func<T>委托的定義是相對于Action<T>來說。Action<T>是沒有返回值得方法委托,Func<T>是有返回值的委托。返回值的類型,由泛型中定義的類型進行約束。例如:

  

復制代碼
        /// <summary>        /// 應用程序的主入口點。        /// </summary>        [STAThread]        static void Main()        {            //異步執行             Func<string> FuncAsy = new Func<string>(() =>            {                people tPeo = new people("異步小李", 10);                return tPeo.ToString();            }            );            FuncAsy.BeginInvoke(resual =>                {                    //異步執行,從回調函數中獲取返回結果                    Console.WriteLine(FuncAsy.EndInvoke(resual));                    Console.WriteLine("異步執行結束");                }, null);            //同步執行             Func<string> Func = new Func<string>(() =>            {                people tPeo = new people("同步小李", 12);                return tPeo.ToString();            }            );            //同步執行,獲取返回結果            Console.WriteLine(Func.Invoke());            Console.Read();        }        public class people        {            public string Name { get; set; }            public int Age { get; set; }            public people(string pName, int pAge)            {                this.Name = pName;                this.Age = pAge;            }            public override string ToString()            {                return string.Format("名稱叫{0},年齡{1}", this.Name, this.Age);            }        }復制代碼

  輸出結果如下:  

  如果有參數,可以這樣寫:

 

復制代碼
        /// <summary>        /// 應用程序的主入口點。        /// </summary>        [STAThread]        static void Main()        {            //異步執行 傳入一個people類型的參數,返回一個sting類型的結果            Func<people, string> FuncAsy = new Func<people, string>((pPeople) =>            {                return pPeople.Name;            }            );            FuncAsy.BeginInvoke(new people("異步小李", 12), resual =>                {                    //異步執行,從回調函數中獲取返回結果                    Console.WriteLine(FuncAsy.EndInvoke(resual));                    Console.WriteLine("異步執行結束");                }, null);            //同步執行 傳入一個string,int類型的參數,返回一個people類型的結果            Func<string, int, people> Func = new Func<string, int, people>((pName,pAge) =>            {                people tPeo = new people(pName, pAge);                return tPeo;            }            );            //同步執行,返回結果            Console.WriteLine(Func.Invoke("同步小李",12).ToString());            Console.Read();        }復制代碼

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湘乡市| 沙坪坝区| 鄂州市| 舞阳县| 陇西县| 南京市| 根河市| 独山县| 汕尾市| 沧源| 鲁山县| 特克斯县| 天长市| 屏南县| 岫岩| 平阳县| 兴国县| 昭苏县| 越西县| 临夏市| 吉水县| 安丘市| 二手房| 洞头县| 兰考县| 安塞县| 昔阳县| 漠河县| 杨浦区| 洛宁县| 清徐县| 东乌珠穆沁旗| 综艺| 西充县| 海淀区| 仙居县| 玉树县| 洪泽县| 祁阳县| 健康| 西畴县|