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

C# Action和 Func的用法

C#中的数据类型 函数数据类型

Action 是一个数据类型 但是是没有返回值得函数数据类型

Func 用于指定一个有返回值的委托

  internal class Program
  {

      static void Main(string[] args)
      {
          TT.F1(NoVoid);
          TT.F2(Void1);
          

          Void2(() => { Console.WriteLine("Void2执行了"); });
      }
      static void NoVoid()
      {
         
      }
      static void Void1(int i)
      {
          Console.WriteLine("值是"+i);
      }
      static void Void2(Action act)
      {
          Console.WriteLine("正在执行Void2");
          act(); //回调 这个委托方法
      }
  }
  class TT
  {
     
      public static void F1(Action act) { }
      public static void F2(Action<int> act) { }
  }

delegate和Action的写法

delegate写法

 public delegate void MyDelegate(string str);

 static void Main(string[] args)
 {

     MyDelegate myDelegate = new MyDelegate(Sum);
     myDelegate("123");
 }
 static void  Sum(string str)
 {
     Console.WriteLine(str);
 }

修改为 Action写法

   static void Main(string[] args)
   {
       Action<int> i = Void1;
       i(5);
   }
  static void Void1(int i)
  {
      Console.WriteLine("i的值是"+i);
  }

Action的写法更简洁


Func

Func是有返回值的

Func<T1>(有返回值) --------T1为返回值的类型,无参数类型

Func<T1,T2>(有返回值)-----T1是参数类型,T2是返回值类型

Func<T1,T2,T3>(有返回值)--T1和T2是参数类型,T3是返回值类型

代码示例

internal class Program
{
    static void Main(string[] args)
    {
        Test.Fn1(fn1);
        Test.Fn2(fn2);
    }
    static int fn1()
    {
        return 12;
    }
    static string fn2(int a)
    {
        return "123456";
    }
}
class Test
{
    public static void Fn1(Func<int> fn) { }
    public static void Fn2(Func<int,string> fn) { }
}

Lambda表达式

 Func<int,int>fn3=y=>y;

 Action<int, int> fn2 = (int x, int y) =>
 {
     Console.WriteLine(x + y);
 };

Func和泛型委托(4种方法)

 delegate bool CallBack(string a);
 delegate bool CallBack<T>(T obj);
 internal class Program
 {
   
     static void Main(string[] args)
     {
         //查询是张字开头的
         string[] names = { "花木兰", "蒙恬", "白起", "张飞", "马超" };
         Console.WriteLine(Test.Main1(names, Fn1));
         Console.WriteLine(Test.Main2<string>(names, Fn1));
         Console.WriteLine(Test.Main3(names, Fn1));
         Console.WriteLine(Test.Main4<string>(names,Fn1));
         Console.WriteLine(Test.MyIndex(names,Fn1));
     }
     public static bool Fn1(string s)
     {
         return s.StartsWith("张");
     }
 }
 class Test
 {
     public static string Main1(string[] args,Func<string,bool>f)
     {
         for (int i = 0; i < args.Length; i++)
         {
             if(f(args[i]))
             {
                 return args[i];
             }
         }
         return default;
     }
     public static T Main2<T>(T[]args,Func<T,bool>f)
     {
         for(int i = 0;i < args.Length;i++)
         {
             if (f(args[i]))
             {
                 return args[i];
             }
         }
         return default;
     }
     public static string Main3(string[]args,CallBack f)
     {
         for (int i = 0; i < args.Length; i++)
         {
             if (f(args[i]))
             {
                 return args[i];
             }
         }
         return default;
     }
     public static T Main4<T>(T[]args,CallBack<T> f)
     {
         for (int i = 0; i < args.Length; i++)
         {
             if (f(args[i]))
             {
                 return args[i];
             }
         }
         return default(T);
     }
     public static int MyIndex(string[] arrs, Func<string, bool> f)
     {
         for (int i = 0; i < arrs.Length; i++)
         {
             if (f(arrs[i]))
             {
                 return i;
             }
         }
         return -1;
     }
 }


http://www.kler.cn/a/533948.html

相关文章:

  • Linux之安装MySQL
  • 修剪二叉搜索树(力扣669)
  • 解决threeJS加载obj gltf和glb模型后颜色太暗的方法
  • 拍照对比,X70 PRO与X90 PRO+的细节差异
  • 大数据挖掘--两个角度理解相似度计算理论
  • [paddle] 矩阵相关的指标
  • [操作系统] 进程终止
  • 大模型 Llama 微调如何适配中文_词表扩展
  • 如何开发一个大语言模型,开发流程及需要的专业知识
  • 【数学】矩阵、向量(内含矩阵乘法C++)
  • Ubuntu22.04如何设置linux-lowlatency核心
  • 扩增子分析|零模型2——基于βNTI的微生物随机性和确定性装配过程(箱线图和柱状图R中实现)
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_sprintf_num 函数
  • 2024年Web前端最新Java进阶(五十五)-Java Lambda表达式入门_eclipse lambda(1),面试必备
  • 高压GaN(氮化镓)器件在工业和汽车应用存在的致命弱点
  • git 设置分支跟踪
  • Nginx通过设置自定义标记识别代理调用
  • VMware Win10下载安装教程(超详细)
  • 《手札·开源篇》基于开源Odoo软件与Deepseek的智能企业管理系统集成方案
  • R语言 | 使用 ComplexHeatmap 绘制热图,分区并给对角线分区加黑边框
  • Noise Conditional Score Network
  • 玩转goroutine:Golang中对goroutine的理解
  • 多用户同时RDP登入Win10
  • 大型三甲医院算力网络架构的深度剖析与关键技术探索
  • JAVA 二维列表的基础操作与异常
  • python实现多路视频,多窗口播放功能