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

C#内置委托(Action)(Func)

概述

在 C# 中,委托是一种类型,它表示对具有特定参数列表和返回类型的方法的引用。C# 提供了一些内置委托,使得开发者可以更方便地使用委托功能,无需手动定义委托类型。本文将详细介绍 Action 和 Func 这两个常用的内置委托。

Action 委托

Action 委托用于表示没有返回值的方法。它可以有 0 到 16 个输入参数,这些参数的类型可以不同。

无参数的 Action 委

// 不支持返回值的内置委托
Action action = new Action(() =>
{Console.WriteLine("无参数委托");
});
action();

在这个例子中,我们创建了一个无参数的 Action 委托 action,并使用 Lambda 表达式为其赋值。当调用 action() 时,会执行 Lambda 表达式中的代码,输出 无参数委托

带参数的 Action 委托

// 带参数不可以有返回值
// 使用方法
Action<string, int> action1 = new Action<string, int>(MyAction);// 使用匿名函数
Action<string, int> action2 = (a, b) => {Console.WriteLine($"我叫{a},今年{b}岁");
};
action2("凡凡", 18);static void MyAction(string a, int b)
{Console.WriteLine($"{a},{b}");
}

这里创建了两个带参数的 Action 委托。action1 委托引用了 MyAction 方法,action2 委托使用了匿名函数。Action<string, int> 表示该委托接受一个 string 类型和一个 int 类型的参数,并且没有返回值。

Func 委托

Func 委托用于表示有返回值的方法。它至少有一个泛型参数,最后一个泛型参数表示返回值类型,前面的泛型参数表示输入参数类型。

无参数的 Func 委托

// 带返回类型的委托
Func<string> func1 = new Func<string>(MyFunc);
Console.WriteLine(func1());Func<string> func2 = () => { return "哈哈"; };
Console.WriteLine(func2());static string MyFunc()
{return "嘿嘿";
}

Func<string> 表示该委托没有输入参数,返回值类型为 stringfunc1 委托引用了 MyFunc 方法,func2 委托使用了匿名函数。

带参数的 Func 委托

// 设置了三个泛型参数类型,前两个代表参数,最后一个代表返回
Func<string, int, bool> func3 = new Func<string, int, bool>(MyFunc2);
Func<string, int, bool> func4 = (a, b) => { return int.Parse(a) == b; };
Console.WriteLine(func3("1", 2));
Console.WriteLine(func4("2", 2));static bool MyFunc2(string a, int b)
{return int.Parse(a) == b;
}

Func<string, int, bool> 表示该委托接受一个 string 类型和一个 int 类型的参数,返回值类型为 boolfunc3 委托引用了 MyFunc2 方法,func4 委托使用了匿名函数。

总结

Action 委托适用于不需要返回值的方法,而 Func 委托适用于需要返回值的方法。通过使用这些内置委托,可以减少手动定义委托类型的工作量,使代码更加简洁和易于维护。


namespace _2.内置委托
{internal class Program{static void Main(string[] args){//不支持返回值的内置委托Action action = new Action(() =>{Console.WriteLine("无参数委托");});action();//带参数不可以有返回值Action<string, int> action1 = new Action<string, int>(MyAction);//使用方法Action<string, int> action2 = (a, b) => {Console.WriteLine($"我叫{a},今年{b}岁");};//使用匿名函数action2("凡凡", 18);//带返回类型的委托Func<string> func1 = new Func<string>(MyFunc);Console.WriteLine(func1());Func<string> func2 =() => { return "哈哈"; };Console.WriteLine(func2());//设置了三个泛型参数类型,前两个代表参数,最后一个代表返回Func<string, int, bool> func3 =new Func<string, int, bool>(MyFunc2);Func<string, int, bool> func4 = (a, b) => { return int.Parse(a) == b; };Console.WriteLine(func3("1",2));Console.WriteLine(func4("2",2));}static void MyAction(string a,int b){Console.WriteLine($"{a},{b}");}static string MyFunc(){return "嘿嘿";}static bool MyFunc2(string a,int b){return int.Parse(a) == b;}}
}

http://www.lryc.cn/news/544980.html

相关文章:

  • kubernetes 部署项目
  • 《几何原本》命题I.2
  • 【我的 PWN 学习手札】House of Kiwi
  • nvm的学习
  • haclon固定相机位标定
  • stm32(hal库)学习笔记-时钟系统
  • 【Java项目】基于SpringBoot的财务管理系统
  • Qt中如果槽函数运行时间久,避免阻塞主线程的做法
  • 曹操智行构建国内首个全域自研闭环智驾生态
  • day02_Java基础
  • SpringSecurity 实现token 认证
  • 轻松实现语音生成:GPT-SoVITS V2整合包的远程访问操作详解
  • 解锁状态模式:Java 编程中的行为魔法
  • 算法与数据结构(相交链表)
  • 浅入浅出Selenium DevTools
  • 软件工程---净室软件工程
  • OpenHarmony图形子系统
  • 如何获取Mac OS 安装盘
  • 【弹性计算】弹性裸金属服务器和神龙虚拟化(一):功能特点
  • 大白话前端性能优化方法的分类与具体实现
  • Rabbit MQ 高频面试题【刷题系列】
  • ES6 特性全面解析与应用实践
  • 有关数据库表的冗余字段
  • 知识图谱补全KGC
  • 独立开发者的内容营销教程
  • Mysql——约束与多表查询
  • DockerでOracle Database 23ai FreeをセットアップしMAX_STRING_SIZEを拡張する手順
  • Unity 运用正则表达式保留字符串中的中文英文字母和数字
  • vue el-table-column 单元表格的 省略号 实现
  • 企业微信里可以使用的企业内刊制作工具,FLBOOK