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 委托一起使用。
相关推荐
一只小小汤圆21 分钟前
如何xml序列化 和反序列化类中包含的类
xml·开发语言·c#
wkj0014 小时前
接口实现类向上转型和向上转型解析
java·开发语言·c#
阿伍.6 小时前
【指针】(适合考研、专升本)
c++·考研·c#
香煎三文鱼6 小时前
C# net8生成excel,并设置列规则导出文件
c#·excel·c#生成ecel
汪小白JIY7 小时前
【C#】多级缓存与多核CPU
缓存·c#·多级缓存
工程师0078 小时前
C#AES加密
网络·安全·web安全·c#
程序设计实验室10 小时前
提升Avalonia UI质感,跨平台图标库选型实践
c#·avalonia
我是苏苏13 小时前
C#高级:利用反射让字符串决定调用哪个方法
后端·c#
月巴月巴白勺合鸟月半18 小时前
工作记录 2017-08-01
c#·健康医疗