zoukankan      html  css  js  c++  java
  • (转)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<>。

  • 相关阅读:
    二分练习题4 查找最接近的元素 题解
    二分练习题5 二分法求函数的零点 题解
    二分练习题3 查找小于x的最大元素 题解
    二分练习题2 查找大于等于x的最小元素 题解
    二分练习题1 查找元素 题解
    code forces 1176 D. Recover it!
    code forces 1173 B. Nauuo and Chess
    code forces 1173 C. Nauuo and Cards
    吴恩达深度学习课程笔记-15
    吴恩达深度学习课程笔记-14
  • 原文地址:https://www.cnblogs.com/wangle1001986/p/12179729.html
Copyright © 2011-2022 走看看