NET Core C# 中的Action委托:语法、用法和示例_2024-06-19

Action委托是一个内置的泛型委托类型。此委托使您的程序更具可读性和效率,因为您无需定义自定义委托,如以下示例所示。

它在 System 命名空间下定义。它没有输出参数,输入参数最少为 1 个,最多为 16 个。

Action委托通常用于具有 void 返回类型的方法,或根本不包含任何类型的值的方法。此外,它可能包含一种或多种类型的参数。

句法

// 1个输入参数
public delegate void Action<in param1>(param1 obj);

// 2个输入参数
public delegate void Action<in param1, in param2>(param1 arg1, param2 arg2);

自定义委托使用例子说明

using System;

public delegate void Sum(int a, int b);

class Program
{
    static void Adds(int a, int b)
    {
        Console.WriteLine("Addition: {0}", a + b);
    }

    static void Main()
    {
        Sum addition = Adds;
        addition(22, 45);

        Console.ReadLine();
    }
}

Action委托使用例子说明

using System;

class Program
{
    static void Main()
    {
        Action<int, int> addition = Add;
        addition(22, 45);

        Console.ReadLine();
    }

    static void Add(int a, int b)
    {
        Console.WriteLine("Addition: {0}", a + b);
    }
}

在上述示例中使用 Action Delegate 可使代码更简洁,软件更易于阅读。此操作委托包含两个输入参数。此外,我们让 Action 委托直接访问 Add 方法。

可以通过直接分配方法或使用 new 关键字来初始化 Action 委托。

句法

using System;

class Program
{
    static void Main()
    {
        // Example 1: Action delegate assignment
        Action<int> objectname = method;

        // Example 2: Action delegate assignment with explicit instantiation
        Action<int> objectname = new Action<int>(method);
    }

    static void method(int param)
    {
        // Method implementation
    }
}

举例来说,

using System;

class Program
{
    static void Main()
    {
        // Example 1: Action delegate assignment
        Action<int, int> addition = Add;
        addition(22, 45);

        // Example 2: Action delegate assignment with explicit instantiation
        var add = new Action<int, int>(Add);
        add(34, 65);

        Console.ReadLine();
    }

    static void Add(int a, int b)
    {
        Console.WriteLine("Addition: {0}", a + b);
    }
}

还可以为 Action 委托分配一个匿名方法,如下例所示。

using System;

class Program
{
    static void Main()
    {
        Action<int, int> addition = delegate (int a, int b)
        {
            Console.WriteLine("Addition: {0}", a + b);
        };

        addition(22, 45);
    }
}

Action 委托也可以与 lambda 表达式一起使用。

using System;

class Program
{
    static void Main()
    {
        Action<int, int> addition = (a, b) => Console.WriteLine("Addition: {0}", a + b);
        addition(22, 45);
    }
}

因此,您可以将 Action 委托类型与任何不返回值的方法一起使用。

Action委托的优点

  1. 定义简单且快捷。
  2. 缩短代码。
  3. 全面兼容的排序。

要记住的关键点

  1. Action委托的功能与 func 托类似,但是它不返回任何内容。返回类型必须为 null。
  2. Action委托输入参数范围是0至16。
  3. Lambda 表达式和匿名方法都可以与 Action 委托一起使用。
相关推荐
新手unity自用笔记2 小时前
项目-坦克大战学习-子弹的移动与销毁
笔记·学习·c#
qinzechen3 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
yufei-coder7 小时前
C# Windows 窗体开发基础
vscode·microsoft·c#·visual studio
dangoxiba7 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
AitTech7 小时前
深入理解C#中的TimeSpan结构体:创建、访问、计算与格式化
开发语言·数据库·c#
hiyo58511 小时前
C#中虚函数和抽象函数的概念
开发语言·c#
开心工作室_kaic13 小时前
基于微信小程序的校园失物招领系统的设计与实现(论文+源码)_kaic
c语言·javascript·数据库·vue.js·c#·旅游·actionscript
时光追逐者17 小时前
WaterCloud:一套基于.NET 8.0 + LayUI的快速开发框架,完全开源免费!
前端·microsoft·开源·c#·.net·layui·.netcore
friklogff19 小时前
【C#生态园】打造现代化跨平台应用:深度解析.NET桌面应用工具
开发语言·c#·.net
hiyo5851 天前
C#的面向对象
开发语言·c#