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 委托一起使用。
相关推荐
偶尔的鼠标人32 分钟前
Avalonia DataGrid 控件的LostFocus事件会多次触发
开发语言·c#
ytttr87344 分钟前
C# 仿QQ聊天功能实现 (SQL Server数据库)
数据库·oracle·c#
future_studio3 小时前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#
c#上位机9 小时前
wpf中Grid的MouseDown 事件无法触发的原因
c#·wpf
CodeCraft Studio10 小时前
国产化PDF处理控件Spire.PDF教程:如何在 C# 中从 HTML 和 PDF 模板生成 PDF
pdf·c#·html·.net·spire.pdf·pdf文档开发·html创建模板pdf
ysdysyn11 小时前
.NET 10深度解析:性能革新与开发生态的全新篇章
c#·.net
L X..14 小时前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
reasonsummer16 小时前
【办公类-115-06】20250920职称资料上传04——docx复制、docx转PDF(课程表11个)
开发语言·windows·python·c#
William_cl1 天前
一、前置基础(MVC学习前提)_核心特性_【C# 泛型入门】为什么说 List<T>是程序员的 “万能收纳盒“?避坑指南在此
学习·c#·mvc
c#上位机1 天前
wpf之命令
c#·wpf