• c# Predicate vs Action vs Func


    Predicate

    定义

    public delegate bool Predicate (P obj);

    Example
    using System;
      
    class GFG {
      
        // Method
        public static bool myfun(string mystring)
        {
            if (mystring.Length < 7)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
      
        // Main method
        static public void Main()
        {
            Predicate val = myfun;
            Console.WriteLine(val("a code cat"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    False

    要点
    • 将 Predicate 委托与匿名方法一起使用
    Predicate val = delegate(string str)
    {
        if (mystring.Length < 7)
        {
            return true;
        }
        else 
        {
            return false;
        };
        val("Geeks");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 使用带有 lambda 表达式的 Predicate 委托
    Predicate val = str = > str.Equals(str.ToLower());
    val("a code cat");
    
    • 1
    • 2

    Action

    定义

    Action delegate is used with those methods whose return type is void。

    // One input parameter
    public delegate void Action < in P > (P obj);

    // Two input parameters
    public delegate void Action < in P1, in P2 >(P1 arg1, P2 arg2);

    using System;
      
    class GFG {
      
        public static void myfun(int p, int q)
        {
            Console.WriteLine(p - q);
        }
      
        static public void Main()
        {
            Action val = myfun;
            val(20, 5);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    重点
    • Action Delegates 和 Function Delegates 之间的唯一区别是 Action Delegates 不返回任何内容

    Action val = new Action(myfun);

    Action val = myfun;

    Action val = delegate(string str)
    {
    Console.WriteLine(str);
    };
    val(“a code cat”);

    Action val = str = > Console.WriteLine(str);
    val(“a code cat”);


    Func

    语法
    public delegate TResult Func();
    
    public delegate TResult Func(P arg);
    
    public delegate TResult Func(P1 arg1, P2 arg2);
    
    public delegate TResult Func(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13, P14 arg14, P15 arg15, P16 arg16);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    Sample
    using System;
      
    class GFG {
      
        public static int method(int num)
        {
            return num + num;
        }
      
        static public void Main()
        {
            Func myfun = method;
            Console.WriteLine(myfun(10));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    重点
    • Func Delegate 中的最后一个参数始终是一个输出参数,它被视为返回类型。 它通常用于结果

    • 将 Func 委托与匿名方法一起使用

    Func val = delegate(int x, int y, int z)
    {
    return x + y + z;
    };

    • 将 Func 委托与 lambda 表达式一起使用

    Func val = (int x, int y, int z) = > x + y + z;

  • 相关阅读:
    【代数学习题4.1】从零理解范数与迹 —— 求极小多项式
    MATLAB算法实战应用案例精讲-【图像处理】计算机视觉
    概率DP—二刷
    微信小程序中监听横屏竖屏
    javascript原生态xhr上传多个图片,可预览和修改上传图片为固定尺寸比例,防恶意代码,加后端php处理图片
    JVS规则引擎,打造智能自动化决策的利器
    Flutter 3.3 正式发布啦
    DPDK系列之三十四DPDK并行机制的同步控制
    (每日一题)[leetCode] 622设计循环队列
    力扣第55题
  • 原文地址:https://blog.csdn.net/a_codecat/article/details/127824986