当前位置: 首页 > 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;
        }
    }
}

相关文章:

  • anaconda配置pytorch
  • 深度学习_第二轮
  • 数据结构——布隆过滤器
  • 天锐绿盾软件|外设管控具体有哪些措施?
  • 正交投影与内积空间:机器学习的几何基础
  • 考研复试问题总结-数据结构(1)
  • 把GB型材库放入solidwork中点击库无法应
  • HTTP与HTTPS:从原理到实践,深入解析Web通信的奥秘
  • 扬帆启航于数据结构算法之雅舟旅程,悠然漫步于C++秘境——Leetcode刷题之用栈实现队列,用队列实现栈
  • Vue3 + Vite + TS,使用 Pinia
  • 程序诗篇里的灵动笔触:指针绘就数据的梦幻蓝图(水文,勿三)
  • 通过 python3 指令如何建立虚拟环境的办法
  • React + TypeScript 实现 SQL 脚本生成全栈实践
  • 数据集笔记:新加坡 一些交通的时间序列统计量
  • 不同版本的BLE和WiFi有什么区别?
  • 【MATLAB例程】三维下的IMM(交互式多模型),模型使用CV(匀速)、CT(匀速转弯)和CA(匀加速),滤波使用EKF。附完整代码
  • LeetCode 每日一题 2025/2/24-2025/3/2
  • 如何把word文档整个文档插入到excel表格里?
  • 内存管理技巧与任务堆栈优化详解(结合六足机器人项目)
  • 火山引擎 DeepSeek R1 API 使用小白教程
  • html5企业网站开发/全网最低价24小时自助下单平台
  • 澳门wap网站制作/重庆seo网站建设
  • 网站续费 多久/网站检测
  • 网页制作与网站建设初学者必看教程/手机百度电脑版入口
  • 用vs2010做网站/微信广点通广告平台
  • 国外知名网站排行/教育机构退费纠纷找谁