当前位置: 首页 > article >正文

C# 匿名函数 delegate(参数...){ }

什么是匿名函数


      顾名思义,就是没有名字的函数
            匿名函数的使用主要是配合委托和事件进行使用
                    脱离委托和事件 是不会使用匿名函数的

基本语法
            delegate (参数列表)
            {
               函数逻辑
            };


何时使用?
            1.函数中传递委托参数时
            2.委托或事件赋值时

使用

 static void Main(string[] args)
 {
    //1.无参无返回
    //这样申明匿名函数 只是在申明函数而已 还没有调用
    //真正调用它的时候 是这个委托容器啥时候调用 就什么时候调用这个匿名函数
    Action a = delegate ()
    {
        Console.WriteLine("匿名函数逻辑");
    };
    
    a();
    //2.有参
    Action<int, string> b = delegate (int a, string b)
    {
        Console.WriteLine(a);
        Console.WriteLine(b);
    };

    b(100, "123");
    //3.有返回值
    Func<string> c = delegate ()
    {
        return "123123";
    };

    Console.WriteLine(c());

    //4.一般情况会作为函数参数传递 或者 作为函数返回值
    Test t = new Test();
    Action ac = delegate ()
    {
        Console.WriteLine("随参数传入的匿名函数逻辑");
    };
    t.Dosomthing(50, ac);
    //  参数传递
    t.Dosomthing(100, delegate ()
    {
        Console.WriteLine("随参数传入的匿名函数逻辑");
    });

    //  返回值
    Action ac2 = t.GetFun();
    ac2();
    //一步到位 直接调用返回的 委托函数
    t.GetFun()();
}


 class Test
 {
     public Action action;

     //作为参数传递时
     public void Dosomthing(int a, Action fun)
     {
         Console.WriteLine(a);
         fun();
     }

     //作为返回值
     public Action GetFun()
     {
         return delegate() {
             Console.WriteLine("函数内部返回的一个匿名函数逻辑");
         };
     }
}

匿名函数缺点

添加到委托或事件容器中后 不记录 无法单独移除 只能置空

            Action ac3 = delegate ()
            {
                Console.WriteLine("匿名函数一");
            };

            ac3 += delegate ()
            {
                Console.WriteLine("匿名函数二");
            };

            ac3();
         // 因为匿名函数没有名字 所以没有办法指定移除某一个匿名函数
       //   此匿名函数 非彼匿名函数 不能通过看逻辑是否一样 就证明是一个 
            //ac3 -= delegate ()
            //{
            //    Console.WriteLine("匿名函数一");
            //};
            ac3 = null;
            //ac3();

练习

写一个函数传入一个整数,返回一个函数

之后执行这个匿名函数时传入一个整数和之前那个函数传入的数相乘

返回结果

 class Program
 {
 public static void Main()
 {
    Console .WriteLine (  First(5)(3));
 }
 public static  Func<int,int> First(int a)
 {
     return delegate (int b)
     {
         return b * a;
     };
 }

 }


http://www.kler.cn/news/283473.html

相关文章:

  • LeetCode 热题100-41 二叉树的层序遍历
  • 如何使用 Pytest 进行测试
  • SSRF和CSRF实战复现
  • 【自动驾驶】决策规划算法概述
  • Kafka消息积压的典型场景及解决方案
  • SSRF以及CSRF
  • Vue3.0项目实战(二)——大事件管理系统登录注册功能实现
  • 快讯 | Midjourney开拓硬件领域:苹果前经理加盟助力发展
  • 防御Nginx负载均衡中的拒绝服务攻击:策略与实践
  • OpenCV详细介绍
  • Eureka的生命周期管理:服务注册、续约与下线的完整流程解析
  • uniapp,uview:inputnumber或者input,当type为number的时候,在ios里输入不了小数的问题
  • 本地部署aniportrait
  • 【Redis】Redis 的消息队列 List、Streams—(六)
  • Golang测试func TestXX(t *testing.T)的使用
  • 【GPT】Coze使用开放平台接口-【2】创建工作流-语音伪造检测工作流
  • Golang | Leetcode Golang题解之第375题猜数字大小II
  • XSS漏洞
  • 数组、向量与矩阵
  • 【Unity优化】优化Android平台拖动地图表现
  • zabbix安装过程中仓库不可用问题解决
  • uniapp中H5网页怎么实现自动点击事件
  • 自动化测试经典面试题
  • 拿到一个新项目,如何开展测试?
  • 基于状态机实现WIFI模组物联网
  • 2.10鼠标事件
  • MySQL(六)——多表查询
  • ZaKi:Ingonyama的Prover market基础设施
  • 基础概念与简单数据结构的笔记02
  • 数据结构---循环队列---树的基本概念