溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#內(nèi)置泛型委托中Action委托是什么

發(fā)布時間:2022-03-14 09:10:50 來源:億速云 閱讀:210 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下C#內(nèi)置泛型委托中Action委托是什么,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1、什么是Action泛型委托

Action<T>是.NET Framework內(nèi)置的泛型委托,可以使用Action<T>委托以參數(shù)形式傳遞方法,而不用顯示聲明自定義的委托。封裝的方法必須與此委托定義的方法簽名相對應(yīng)。也就是說,封裝的方法必須具有一個通過值傳遞給它的參數(shù),并且不能有返回值。

2、Action委托定義

查看Action的定義:

using System.Runtime.CompilerServices;

namespace System
{
    //
    // 摘要:
    //     封裝一個方法,該方法不具有參數(shù)且不返回值。
    [TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")]
    public delegate void Action();
}

你會發(fā)現(xiàn),Action其實就是沒有返回值的delegate。

3、示例

Action委托至少0個參數(shù),至多16個參數(shù),無返回值。

Action 表示無參,無返回值的委托。

Action<int,string> 表示有傳入?yún)?shù)int,string無返回值的委托。

Action<int,string,bool> 表示有傳入?yún)?shù)int,string,bool無返回值的委托。

Action<int,int,int,int> 表示有傳入4個int型參數(shù),無返回值的委托。

C#內(nèi)置泛型委托中Action委托是什么

代碼示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ActionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 無參數(shù)無返回值的委托
            Action action1 = new Action(ActionWithNoParaNoReturn);
            action1();
            Console.WriteLine("----------------------------");
            // 使用delegate
            Action action2 = delegate { Console.WriteLine("這里是使用delegate"); };
            // 執(zhí)行
            action2();
            Console.WriteLine("----------------------------");
            // 使用匿名委托
            Action action3 = () => { Console.WriteLine("這里是匿名委托"); };
            action3();
            Console.WriteLine("----------------------------");
            // 有參數(shù)無返回值的委托
            Action<int> action4 = new Action<int>(ActionWithPara);
            action4(23);
            Console.WriteLine("----------------------------");
            // 使用delegate
            Action<int> action5 = delegate (int i) { Console.WriteLine($"這里是使用delegate的委托,參數(shù)值是:{i}"); };
            action5(45);
            Console.WriteLine("----------------------------");
            // 使用匿名委托
            Action<string> action6 = (string s) => { Console.WriteLine($"這里是使用匿名委托,參數(shù)值是:{s}"); };
            action6("345");
            Console.WriteLine("----------------------------");
            // 多個參數(shù)無返回值的委托
            Action<int, string> action7 = new Action<int, string>(ActionWithMulitPara);
            action7(7, "abc");
            Console.WriteLine("----------------------------");
            // 使用delegate
            Action<int, int, string> action8 = delegate (int i1, int i2, string s) 
            {
                Console.WriteLine($"這里是三個參數(shù)的Action委托,參數(shù)1的值是:{i1},參數(shù)2的值是:{i2},參數(shù)3的值是:{s}");
            };
            action8(12, 34, "abc");
            Console.WriteLine("----------------------------");
            Action<int,int,string, string> action9 = (int i1,int i2, string s1,string s2) => 
            {
                Console.WriteLine($"這里是使用四個參數(shù)的委托,參數(shù)1的值是:{i1},參數(shù)2的值是:{i2},參數(shù)3的值是:{s1},參數(shù)4的值是:{s2}");
            };
            // 執(zhí)行委托
            action9(34,56, "abc","def");
            Console.ReadKey();
        }




        static void ActionWithNoParaNoReturn()
        {
            Console.WriteLine("這是無參數(shù)無返回值的Action委托");
        }

        static void ActionWithPara(int i)
        {
            Console.WriteLine($"這里是有參數(shù)無返回值的委托,參數(shù)值是:{i}");
        }

        static void ActionWithMulitPara(int i,string s)
        {
            Console.WriteLine($"這里是有兩個參數(shù)無返回值的委托,參數(shù)1的值是:{i},參數(shù)2的值是:{s}");
        }
    }
}

運行結(jié)果:

C#內(nèi)置泛型委托中Action委托是什么

4、真實示例

先看下面一張截圖:

C#內(nèi)置泛型委托中Action委托是什么

從截圖中可以看出:ForEach()方法的參數(shù)是一個參數(shù)類型是T的無返回值的Action委托,下面的示例中利用Action委托作為參數(shù)傳遞給ForEach()方法。

1、定義Student實體類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ActionDemo
{
    public class Student
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public int Sex { get; set; }
    }
}

2、利用ForEach()方法輸出集合內(nèi)容

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ActionDemo
{
    public class ActionTest
    {
        public static void Test()
        {
            List<Student> list = new List<Student>()
            {
              new Student(){Id=1,Name="張三",Age=19,Sex=1},
              new Student(){Id=2,Name="李四",Age=20,Sex=2},
              new Student(){Id=3,Name="王五",Age=23,Sex=1},
              new Student(){Id=4,Name="趙六",Age=18,Sex=1}
            };

            // Action<Student>委托作為參數(shù)傳遞給ForEach()方法
            list.ForEach(student =>
            {
                Console.WriteLine($"姓名:{student.Name},年齡:{student.Age}");
            });
        }
    }
}

3、在Main()方法中調(diào)用

ActionTest.Test();

4、結(jié)果

C#內(nèi)置泛型委托中Action委托是什么

以上是“C#內(nèi)置泛型委托中Action委托是什么”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI