.net 委托

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

委托能够自定义,也可使用内置委托学习

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

无返回值委托:           string

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

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

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

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

//根据源码编写委托foreach

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

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);                 }             }         } }

相关文章
相关标签/搜索