温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

什么是.net 委托

发布时间:2021-07-06 10:34:45 来源:亿速云 阅读:200 作者:chen 栏目:大数据

本篇内容主要讲解“什么是.net 委托”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“什么是.net 委托”吧!

最近手头的活,干的比较快,每天都有时间学习,这几天一直在看委托,做个总结,以后要是模糊了,看看能记起来,没搞懂使用委托的情景。

委托可以自定义,也可以使用内置委托

自定义委托:delegate T4 AllDeleate<in T1, in T2, in T3, out T4>(T1 a, T2 b, T3 c);//是根据查看源码摸你写的委托

无返回值委托:           

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

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

只有一个参数返回布尔类型委托:

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

//根据源码编写委托

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 "根据源码编写委托,并输出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委托匿名函数:" + pre(10));

Predicate<int> preTest = GetBool;
Console.WriteLine("predicate委托函数:" + 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 委托”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI