Action<T>和Func<T>委托

 泛型Action<T>委托和Func<T>委托是系统定义的两个泛型委托。

        Action<T>委托表示引用一个返回类型为Void的方法。这个委托存在不同的变体,可以传递之多16个不同的参数类型。同时,没有泛型参数的Action类可以调用没有参数的方法。例如,Action<in T>表示有一个输入参数的方法,Action<in T1,in T2>表示有两个输入参数的方法。

        Func<T>可以以类似的方法使用。不过Func<T>允许调用带返回参数的方法。Func<T>也有不同的变体,之多可以传递16个参数和一个返回类型。例如:Func<out TResult>委托类型可以无参的带返回类型的方法,Func<in T1,inT2,out Tresult>表示带两个参数和一个返回类型的方法。

        需要记住一个东西,Action<T>中的T可以有多个,但这些T类型都表示不同的输入类型。Func<T>可以表示带输出的方法,T可以有多个,且只有最后一个表示输出即最后一个是返回类型。Func<in T1,inT2,out Tresult>中的字符in、out在实际代码中是不会出现的。在VS中,可以通过IntelliSense查看:

Action<T> 的16参数封装:

 

下边通过一个简单的代码演示一下这两个泛型委托与一般委托的异同。

第一步:先定义俩函数:

public double MultiplyByTwo(double x)
{
    return x * 2;
}
public double Square(double x)
{
    return x * x;
}

 

这俩函数有共同的特征:输入和返回类型都是double

第二步:定义委托数组,并用这两个方法的方法名初始化数组:

delegate double DoubleOp(double x);

DoubleOp[] MyDoubleOp = 
           {
               this.MultiplyByTwo,
               this.Square
           };


Func<double, double>[] myFunc =     //
          {
              this.MultiplyByTwo,
              this.Square
          };

 

最后:查看输出

for (int i = 0; i < MyDoubleOp.Length; i++)
{
    Console.WriteLine(MyDoubleOp[i](1.414));
}


for (int i = 0; i < myFunc.Length; i++)
{
    Console.WriteLine(myFunc[i](2.236));
}

 

比较一下,其实泛型委托和自定义的委托在使用上没什么不同。只不过泛型委托Func<T>系统已经为我们定义好了,直接使用就可以了,不需要再进行这样的定义delegate double DoubleOp(double x);。另一个泛型委托Action <T>的使用也是一样的,只不过不能有返回类型而已。

 

 

 

装一个具有一个参数并返回 TResult 参数指定的类型值的方法。

命名空间:  System
程序集:  System.Core(在 System.Core.dll 中)

语法
public delegate TResult Func<T, TResult>(
 T arg
)
类型参数
T
此委托封装的方法的参数类型。

TResult
此委托封装的方法的返回值类型。

参数
arg
类型:T

此委托封装的方法的参数。

返回值
类型:TResult

此委托封装的方法的返回值。

备注
可以使用此委托构造一个能以参数形式传递的方法,而不用显式声明自定义的委托。该方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且必须返回值。

注意:
若要引用具有一个参数并返回 void 的方法(或者要在 Visual Basic 中引用被声明为 Sub 而不是被声明为 Function 的方法),请改用泛型 Action<T> 委托。
 

在使用 Func<T, TResult>委托时,不必显式定义一个封装只有一个参数的方法的委托。例如,以下代码显式声明了一个名为 ConvertMethod 的委托,并将对 UppercaseString 方法的引用分配给其委托实例

 
using System;

delegate string ConvertMethod(string inString);

public class DelegateExample
{
   public static void Main()
   {
      // Instantiate delegate to reference UppercaseString method
      ConvertMethod convertMeth = UppercaseString;
      string name = "Dakota";
      // Use delegate instance to call UppercaseString method
      Console.WriteLine(convertMeth(name));
   }

   private static string UppercaseString(string inputString)
   {
      return inputString.ToUpper();
   }
}

以下示例简化了此代码,它所用的方法是实例化 Func<T, TResult> 委托,而不是显式定义一个新委托并将命名方法分配给该委托

using System;

public class GenericFunc
{
   public static void Main()
   {
      // Instantiate delegate to reference UppercaseString method
      Func<string, string> convertMethod = UppercaseString;
      string name = "Dakota";
      // Use delegate instance to call UppercaseString method
      Console.WriteLine(convertMethod(name));
   }

   private static string UppercaseString(string inputString)
   {
      return inputString.ToUpper();
   }
}

 

您也可以按照以下示例所演示的那样在 C# 中将 Func<T, TResult> 委托与匿名方法一起使用。(有关匿名方法的简介,请参见匿名方法(C# 编程指南)。)
using System;

public class Anonymous
{
   public static void Main()
   {
      Func<string, string> convert = delegate(string s)
         { return s.ToUpper();};

      string name = "Dakota";
      Console.WriteLine(convert(name));  
   }
}

 

您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Func<T, TResult>委托。(有关 lambda 表达式的简介,请参见 Lambda 表达式和 Lambda 表达式(C# 编程指南)。)

using System;

public class Lambdaexpression_r_r
{
   public static void Main()
   {
      Func<string, string> convert = s => s.ToUpper();

      string name = "Dakota";
      Console.WriteLine(convert(name));  
   }
}

 

Lambda 表达式的基础类型是泛型 Func 委托之一。这样能以参数形式传递 lambda 表达式,而不用显式将其分配给委托。尤其是,因为 System.Linq 命名空间中许多类型方法具有 Func<T, TResult> 参数,因此可以给这些方法传递 lambda 表达式,而不用显式实例化 Func<T, TResult> 委托。

示例
下面的示例演示如何声明和使用 Func<T, TResult>委托。此示例声明一个 Func<T, TResult>变量,并为其分配了一个将字符串中的字符转换为大写的 lambda 表达式。随后将封装此方法的委托传递给 Select 方法,以将字符串数组中的字符串更改为大写。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

static class Func
{
   static void Main(string[] args)
   {
      // Declare a Func variable and assign a lambda expression_r_r to the 
      // variable. The method takes a string and converts it to uppercase.
      Func<string, string> selector = str => str.ToUpper();

      // Create an array of strings.
      string[] words = { "orange", "apple", "Article", "elephant" };
      // Query the array and select strings according to the selector method.
      IEnumerable<String> aWords = words.Select(selector);

      // Output the results to the console.
      foreach (String word in aWords)
         Console.WriteLine(word);
   }
}     

转载于:https://www.cnblogs.com/wangchuang/p/4488210.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值