溫馨提示×

溫馨提示×

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

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

什么是.net 委托

發(fā)布時間:2021-07-06 10:34:45 來源:億速云 閱讀:205 作者:chen 欄目:大數(shù)據(jù)

本篇內容主要講解“什么是.net 委托”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“什么是.net 委托”吧!

最近手頭的活,干的比較快,每天都有時間學習,這幾天一直在看委托,做個總結,以后要是模糊了,看看能記起來,沒搞懂使用委托的情景。

委托可以自定義,也可以使用內置委托

自定義委托:delegate T4 AllDeleate<in T1, in T2, in T3, out T4>(T1 a, T2 b, T3 c);//是根據(jù)查看源碼摸你寫的委托

無返回值委托:           

Action<string> action = (a) =>
{
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(a + i);
    }
};

有返回值委托:Func<string, string> func = (a) => a + "你好";

只有一個參數(shù)返回布爾類型委托:

Predicate<int> pre = (d) =>
 {
    int tag = 10;
    bool bo = d > tag ? true : false;
     return bo;
};

//根據(jù)源碼編寫委托

var dele = new AllDeleate<string, string, string, string>(TestDeleage);
var str = dele("我是a", "我是b", "我是c");
Console.WriteLine(str);

public string TestDeleage(string a, string b, string c)
{
    return "根據(jù)源碼編寫委托,并輸出a:" + a + ",b:" + b + ",c:" + c;
}

//內置委托,并遍歷委托輸出
Func<string, string> func = GetName;//聲明委托并注冊
func += (d) => d + "我是匿名委托";//注冊
var deleteArray = func.GetInvocationList();//多路廣播委托調用列表
foreach (Func<string, string> item in deleteArray)
{
    var mag = item("小李");
    Console.WriteLine(mag);
}

private string GetName(string name)
{
    Console.WriteLine("委托調用方法");
    return name + "你好,歡迎使用委托";
 }

//predicate委托
Predicate<int> pre = (d) =>
{
    int tag = 10;
    bool bo = d > tag ? true : false;
    return bo;
};
Console.WriteLine("predicate委托匿名函數(shù):" + pre(10));

Predicate<int> preTest = GetBool;
Console.WriteLine("predicate委托函數(shù):" + GetBool(12));

private static bool GetBool(int num)

{

int tag = 10;

bool bo = num > tag ? true : false;

return bo;

}

//編寫委托
var nums = TsetReturn("小明", GetName);
Console.WriteLine(nums);

 public static T2 TsetReturn<T1, T2>(T1 t, Func<T1, T2> func)
 {
    Console.WriteLine("進入方法");
    T2 num = func(t);
    return num;
}

private static string GetName(string name)
{
    Console.WriteLine("委托調用方法");
    return name + "你好,歡迎使用委托";
}

//委托實際應用

public static void DiaoYongTest()
 {
            DelteateClass delteateClass = new DelteateClass();
            delteateClass.td = new TestDeleate(delegate (int i, int maxValue)
            {
                Console.WriteLine("當前進度:" + i);
                Console.WriteLine("最大值:" + maxValue);
            });
            delteateClass.td += new TestDeleate((a, b) =>
            {
                Console.WriteLine("Lambda當前進度:" + a);
                Console.WriteLine("Lambda最大值:" + b);
            });

            delteateClass.td += new TestDeleate(GetProgress);

            delteateClass.ApplyTest();
 }

private static void GetProgress(int i, int maxValue)
{
    Console.WriteLine("GetProgress當前進度:" + i);
    Console.WriteLine("GetProgress最大值:" + maxValue);
 }

class DelteateClass
 {
        public TestDeleate td;
        public void ApplyTest()
        {
            int maxValue = 10;
            var list = td.GetInvocationList();
            foreach (var item in list)
            {
                for (int i = 0; i < maxValue; i++)
                {
                    item?.DynamicInvoke(i, maxValue);
                }
            }
        }
}

到此,相信大家對“什么是.net 委托”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

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

AI