(转)C# 泛型委托 Action<>和Func<>

转:https://www.cnblogs.com/wyongqi/p/7483748.html

如果我们希望定义一个委托类型来调用任何返回void并且接受单个参数的方法。如果这个参数可能会不同,我们就可以通过类型参数来构建。

下面我们看一个小示例:

复制代码
public class GenericDelegate
{
    public delegate void MyGenericDelegate<T>(T arg);

    public static void Show()
    {
     // 注册目标 MyGenericDelegate<string> stringTarget = new MyGenericDelegate<string>(StringTarget); stringTarget("i am ok"); MyGenericDelegate<int> intTarget = new MyGenericDelegate<int>(IntTarget); intTarget.Invoke(100); } static void StringTarget(string arg) => Console.WriteLine($"StringTarget--> {arg.ToUpper()}"); static void IntTarget(int arg) => Console.WriteLine($"IntTarget--> {++arg}"); }
复制代码

a. 泛型Action<> 和 Func<> 委托
从以上的学习中我们已经了解到,使用委托在应用程序中进行回调需要遵循以下步骤:

  • 自定义一个与要指向的方法格式相匹配的委托
  • 创建自定义委托的实例,将方法名作为构造函数的参数
  • 通过调用委托对象的Invoke()方法来间接调用该方法

 其实,这种方式通常会构建大量只用于当前任务的自定义委托。当委托名无关紧要的时候,我们可以使用框架内置的Action<> 和 Func<> 泛型委托,可指向至多传递16个参数的方法。

 Action<>:无返回值: 定义 public delegate void Action<...>

复制代码
public class MyActionDelegate
{
    public static void Show()
    {
        // 使用Action<>委托来指向 DisplayMessage()
        Action<string, ConsoleColor, int> actionTarget = new Action<string, ConsoleColor, int>(DisplayMessage);
        actionTarget("actionTarget", ConsoleColor.Red, 5);
    }

    // Action<> 委托的一个目标
    private static void DisplayMessage(string msg, ConsoleColor txtColor, int printCount)
    {
        ConsoleColor previous = Console.ForegroundColor;
        Console.ForegroundColor = txtColor;

        for (int i = 0; i < printCount; i++)
        {
            Console.WriteLine(msg);
        }

        Console.ForegroundColor = previous;
    }
}
复制代码

运行效果如下图:

Func<>:有返回值 public delegate TResult Func<..., out TResult>

Func的 参数列表中,最后一个是返回类型,前面的都是参数类型

复制代码
public class FuncDelagate
{
    public static void Show()
    {
        Func<int, int, int> funcTarget = new Func<int, int, int>(Add);
        int result = funcTarget(1, 2);
        Console.WriteLine(result);

        Func<int, int, string> funcTarget2 = new Func<int, int, string>(SumToString);
        string sumStr = funcTarget2(3, 4);
        Console.WriteLine(sumStr);
    }
        
    static int Add(int x, int y) => x + y;

    static string SumToString(int x, int y) => (x + y).ToString();
}
复制代码

运行结果:3,7

鉴于 Action<> 和 Func<> 节省了手工创建自定义委托的步骤,but 总是应该使用他们吗?

答案:“视情况而定”。

很多情况下 Action<> 和 Func<> 都是首选,但如果你觉得一个具有自定义名称的委托更有助于捕获问题范畴,那么构建自定义委托不过就是一行代码的事儿。

注:Linq中就大量的用到了 Action<> 和 Func<>。

猜你喜欢

转载自www.cnblogs.com/wangle1001986/p/12179729.html