C# Action和 Func的用法

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

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

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

cs 复制代码
  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写法

cs 复制代码
 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写法

cs 复制代码
   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是返回值类型

代码示例

cs 复制代码
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表达式

cs 复制代码
 Func<int,int>fn3=y=>y;

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

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

cs 复制代码
 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;
     }
 }
相关推荐
黄贵根8 小时前
JavaScript实现教培行业意向登记系统
开发语言·javascript·ecmascript
zzzll111111 小时前
Claude Code离线安装方案揭秘
开发语言·php
wling030112 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
郝学胜-神的一滴12 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
xrandzj13 小时前
Python面向对象编程入门:类、实例、初始化与封装实践
开发语言·python
DLYSB_14 小时前
API 网关流量洪峰与突发 CC 攻击:我用 Go 写了个“现场物理防御哨兵”,把故障响应压缩到秒级
开发语言·后端·golang·报警灯
雨田言炎16 小时前
四、关于Qt项目需要知道的
开发语言·笔记·qt
猿长大人16 小时前
C# | JSON 序列化中的多态接口处理:基于 Attribute 实现精准 $type 控制
c#·json
程序喵大人16 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
SomeB1oody17 小时前
【RustyML入门】2.6. 线性判别分析
开发语言·后端·机器学习·rust·教程