1. 程式人生 > 其它 >C# 泛型委託

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委託