Func<T>与Action<T>委托泛型

Func<T>与Action<T>委托泛型介绍

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

文章摘自:作者: cglnet 出处: http://www.cnblogs.com/cglnet

时间: 2024-08-23 12:36:21

Func<T>与Action<T>委托泛型的相关文章

委托, 泛型委托,Func&lt;T&gt;和Action&lt;T&gt;

使用委托来做一些事情,大致思路是: 1.定义声明一个委托,规定输入参数和输出类型.2.写几个符合委托定义的方法.3.把方法列表赋值给委托4.执行委托 internal delegate int MyDelegate(); class Program { static void Main(string[] args) { MyDelegate d = ReturnOne; d += ReturnTwo; foreach (int i in GetAllReturnVals(d)) { Consol

Action&lt;T&gt; 委托使用详解

Action<T> 委托其实与[button url="http://redcat7.net/?p=343" style="dark"]Func<T, TResult> 委托[/button]一样,都是为了简化委托的使用,这两者的不同之处在于Action<T> 封装一个方法且该方法只有一个参数并且不返回值,而Func<T, TResult>封装一个具有一个参数并返回 TResult 参数指定的类型值的方法. 使用 Ac

.net中用Action等委托向外传递参数

原文:.net中用Action等委托向外传递参数      一般我们可以使用ref,out达到向外传递参数目的. Action<T>是一个特殊的委托,除了常规应用.我们还可以用它来实现简单地向外传递参数.直接看下面的UnitTest代码: 1: [TestMethod] 2: public void PassOutParametersUsingDelegate() 3: { 4: int i = 0; 5: string messgae = string.Empty; 6: int? adde

委托delegate 泛型委托action&lt;&gt; 返回值泛型委托Func&lt;&gt; 匿名方法 lambda表达式 的理解

1.使用简单委托 namespace 简单委托 { class Program { //委托方法签名 delegate void MyBookDel(int a); //定义委托 static MyBookDel myBookDel; //普通方法 public static void MathBook(int a) { Console.WriteLine("我是数学书" + a); } static void Main(string[] args) { myBookDel += Ma

Func&lt;T&gt;、Action&lt;T&gt; 的区别于说明

一.Func Func是一个.Net内置的委托. Func<Result>,Func<T1,Result>是一个.Net内置的泛型委托. Func<TResult> Func<T,TResult> Func<T1,T2,TResult> Func<T1,T2,T3,TResult> Func<T1,T2,T3,T4,TResult> 它有5种形式,只是参数个数不同:第一个是无参数,但是有返回值: 下面是一个简单的普通委托来

Action匿名委托示例

class Program { static void Main(string[] args) { string mid = ",mid"; Action<string> anonDel = delegate (string param) { param += mid; param += " and this."; Console.WriteLine(param); }; anonDel += delegate (string t) { Console.

C#委托的介绍(delegate、Action、Func、predicate)

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public delegate int MethodtDelegate(int x, int y);表示有两个参数,并返回int型. (2). Action Action是无返回值的泛型委托. Action 表示无参,

Func&lt;T,TResult&gt;泛型委托

描述: 封装一个具有一个参数并返回TResult参数指定的类型值的方法. 语法: public delegate TResult Func<T,TResult>(T arg); 参数类型: T:此委托封装的方法的参数类型. TResult:此委托封装的方法的返回值类型. 参数: arg:委托封装的方法的参数 返回值:此委托封装的方法的返回值 备注: 可以使用此委托构造一个能以参数形式传递的方法,而不用显式声明自定义的委托.该方法必须与此委托的方法签名想对应. 也就是说,封装的方法必须具有一个通

C#委托(delegate、Action、Func、predicate)

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public delegate int MethodtDelegate(int x, int y);表示有两个参数,并返回int型. (2). Action Action是无返回值的泛型委托. Action 表示无参,