C# 泛型委托

自定义泛型委托

自定义泛型委托:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 泛型委托
{
    static class Program
    {
        public delegate void MD<T>(T args); //  自定义泛型委托

        static void Main()
        {
            MD<string> mD = M2;
            // MD<string> mD = new MD<string>(M2); // 这种方式也可以。
            mD("我是自定义泛型委托。。。");
        }


        static void M2(string args)
        {
            Console.WriteLine(args);
        }
    }
}

输出:

我是自定义泛型委托。。。
请按任意键继续. . .

自己写程序的时候没有必要自己定义委托,因为 .Net 里面就已经有了。

Action<>

Action<T> 的泛型有 16 个重载:

Action委托,的非泛型版本,就是一个无参数无返回值的委托。

Action<> 泛型版本,就是一个无返回值,但是参数可以变化的委托。

namespace 泛型委托
{
    class Program
    {
        static void Main()
        {
            Action<string> a1 = (str) => { Console.WriteLine(str); };
            a1("我是 Action 泛型委托 a1。。。");

            Action<int, int> a2 = (a, b) => { Console.WriteLine($"a + b = {a + b}"); };
            a2(1, 2);
        }
    }
}

输出:

我是 Action 泛型委托 a1。。。
a + b = 3
请按任意键继续. . .

Func<>

Func<>有没有参数不一定,一定是有返回值的。

Func委托只有一个泛型版本的,没有非泛型版本的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 泛型委托
{
    class Program
    {
        static void Main()
        {
            // 泛型的最后一个参数类型是返回值类型
            Func<int, int, int, double> f1 = ( a,  b,  c) => {
                double fTemp = Convert.ToDouble(a + b + c);
                return fTemp;
            };

            double result = f1(1, 2, 3);
            Console.WriteLine(result);
        }
    }
}

输出:

6
请按任意键继续. . .

Predicate<>

bool 类型的委托。

Predicate<T>委托定义如下:

public delegate bool Predicate<T>(T obj)

解释:此委托返回一个bool值的方法

在实际开发中,Predicate<T>委托变量引用一个判断条件函数,在判断条件函数内部书写代码表明函数参数所引用的对象应该满足的条件,条件满足时返回true

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 泛型委托
{
    class Student
    {
        public int StudentId {
            get;
            set;
        }
        public  string StudentName {
            get;
            set;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            List<Student> stuList = new List<Student>() {
                new Student(){ StudentId = 1001, StudentName = "小张" },
                new Student(){ StudentId = 1008, StudentName = "小李" },
                new Student(){ StudentId = 1009, StudentName = "小王" },
                new Student(){ StudentId = 1003, StudentName = "小赵" },
                new Student(){ StudentId = 1005, StudentName = "小刘" }
            };
            // List<T> 集合中定义了一个 FindAll 方法:public T FindAll(Predicate<T> match)
            List<Student> list = stuList.FindAll(s => s.StudentId > 1003);

            foreach (Student item in list) {
                Console.WriteLine(item.StudentId  + item.StudentName);
            }
            Console.ReadLine();
        }
    }
}

输出:

1008小李
1009小王
1005小刘
请按任意键继续. . .



参考:
1.link-01 // 泛型委托 -> 赵晓虎
2.link-02 // P8第20节-6.Predicate委托

posted @ 2021-09-22 18:42  double64  阅读(276)  评论(0编辑  收藏  举报