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 委托一起使用。
相关推荐
阿蒙Amon几秒前
C# Linq to SQL:数据库编程的解决方案
数据库·c#·linq
iCxhust3 小时前
c# U盘映像生成工具
开发语言·单片机·c#
emplace_back4 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
阿蒙Amon5 小时前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#
深海潜水员6 小时前
【Behavior Tree】-- 行为树AI逻辑实现- Unity 游戏引擎实现
游戏·unity·c#
开开心心_Every6 小时前
便捷的Office批量转PDF工具
开发语言·人工智能·r语言·pdf·c#·音视频·symfony
小码编匠8 小时前
C# 上位机开发怎么学?给自动化工程师的建议
后端·c#·.net
钢铁男儿8 小时前
C# 接口(什么是接口)
java·数据库·c#
小老鼠爱大米12 小时前
C# WPF - Prism 学习篇:搭建项目(一)
c#·wpf·prism