码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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;

  • 相关阅读:
    羊驼笔记:清算bot
    03--nginx架构实战
    IB数学改革后的大纲内容详解
    代码随想录算法训练营第四十八天 | LeetCode 121. 买卖股票的最佳时机、122. 买卖股票的最佳时机 II
    LeetCode每日一题——1704. 判断字符串的两半是否相似
    推荐系统在腾讯游戏运营中的实践
    SDUT OJ《算法分析与设计》贪心算法
    AI智能识别微信小程序源码带流量主功能
    关于游戏介绍的HTML网页设计 HTML5期末考核大作业 HTML静态游戏网页作业 web前端开发技术 web课程设计 网页规划与设计
    方案设计|汽车轮胎数显胎压计方案
  • 原文地址:https://blog.csdn.net/a_codecat/article/details/127824986
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号