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

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 委托一起使用。
  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢.锋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值