一、什麼是委托: 委托是定址方法的.NET版本,使用委托可以將方法作為參數進行傳遞。委托是一種特殊類型的對象,其特殊之處在於委托中包含的只是一個活多個方法的地址,而不是數據。 二、使用委托: 關鍵字:delegate 1.聲明: public delegate void DoNothing();// ...
一、什麼是委托: 委托是定址方法的.NET版本,使用委托可以將方法作為參數進行傳遞。委托是一種特殊類型的對象,其特殊之處在於委托中包含的只是一個活多個方法的地址,而不是數據。 二、使用委托: 關鍵字:delegate 1.聲明: public delegate void DoNothing();//定義一個無返回值,無參數的委托 public delegate int GetNum(int i); //定義有一個返回值int ,參數int 的委托 2.創建方法: public static void DoSome()//無參無返回值的方法
{
Console.WriteLine("DoSome");
} public static int TotalNum(int num)//有一個返回值int ,參數int 的方法
{
return num * num;
} 3.註冊委托: DoNothing doNothing = new DoNothing(DoSome); //或者直接寫出DoNothing doNothing = DoSome; GetNum getNum = AddNum;//註冊委托 4.執行委托 doNothing.Invoke();//執行委托 也可以直接 doNothing(); Console.WriteLine(getNum.Invoke(10));//執行委托並且列印 三、委托的意義 傳遞方法;把方法包裹起來, 傳遞邏輯。非同步多線程執行 四、.net framework3.5之後,系統定義好了2個委托,開發儘量使用框架自帶委托,儘量使用Action和Func Action 無返回值委托,Func 有返回值委托 Action要使用參數,就寫Action<int,string,double> 最多可以到16個 Func要使用參數,就寫成Func<int,string,double> 最多可以到17個, 最後一個為返回值,現在這個返回的就是double類型 Action act = DoSome;//Action 無返回值委托 act.Invoke(); Func<int,int> func = new Func<int,int>(TotalNum) ; func(10); 五、多播委托 Action doSome = new Action(DoSome);
doSome += new Action(DoSome);
doSome += DoSome; doSome();//按順序執行,最後結果是執行3次DoSome方法 doSome -= DoSome;//減少一次DoSome執行 doSome();//按順序執行,最後結果是執行2次DoSome方法 多播委托,按順序執行,多播委托,用Action, Func帶返回值的只執行完後,只得到最後一個結果,所以沒有意義。 委托使用案例:一個學生類,一個學生管理靜態類,可以通過委托,實現學生集合的篩選
public class Student { public int Id { get; set; } public string Name { get; set; } public int ClassId { get; set; } public int Age { get; set; } } public static class StudentManager { public static List<Student> students = new List<Student>() { new Student(){ Id=1,Name="張三",ClassId=1001,Age=15 }, new Student(){ Id=2,Name="李四",ClassId=1001,Age=15 }, new Student(){ Id=3,Name="王五",ClassId=1001,Age=15 }, new Student(){ Id=4,Name="趙六",ClassId=1001,Age=15 }, new Student(){ Id=5,Name="楊冪",ClassId=1001,Age=14 }, new Student(){ Id=6,Name="範冰冰",ClassId=101,Age=14 }, new Student(){ Id=7,Name="張學友",ClassId=1021,Age=14}, new Student(){ Id=8,Name="張三1",ClassId=1021,Age=16 }, new Student(){ Id=9,Name="張三2",ClassId=1001,Age=17 }, new Student(){ Id=10,Name="張三3",ClassId=1001,Age=15 }, new Student(){ Id=11,Name="張三4",ClassId=1001,Age=19 }, new Student(){ Id=12,Name="張三5",ClassId=1001,Age=25 }, new Student(){ Id=13,Name="張三6",ClassId=1003,Age=25 }, new Student(){ Id=14,Name="張三7",ClassId=1003,Age=25 }, new Student(){ Id=15,Name="張三8",ClassId=1003,Age=25 }, new Student(){ Id=16,Name="張三9",ClassId=1003,Age=25 }, new Student(){ Id=17,Name="張三0",ClassId=1003,Age=25 }, new Student(){ Id=18,Name="張三11",ClassId=1003,Age=15 }, new Student(){ Id=19,Name="張三a",ClassId=1011,Age=15 }, new Student(){ Id=20,Name="張三b",ClassId=1011,Age=15 }, new Student(){ Id=21,Name="張三c",ClassId=1011,Age=15 }, new Student(){ Id=22,Name="張三d",ClassId=1011,Age=15 }, new Student(){ Id=23,Name="張三e",ClassId=1011,Age=15 }, new Student(){ Id=24,Name="張三f",ClassId=1011,Age=15 }, new Student(){ Id=25,Name="張三g",ClassId=3001,Age=15 }, new Student(){ Id=26,Name="張三h",ClassId=3001,Age=13 }, new Student(){ Id=27,Name="張三i",ClassId=3001,Age=13 }, new Student(){ Id=28,Name="張三j",ClassId=3001,Age=13 }, new Student(){ Id=29,Name="張三k",ClassId=3001,Age=13 }, }; public static List<Student> FindStudents(Func<Student,bool> func) { List<Student> stus = new List<Student>(); foreach (var item in students) { if (func(item)) { stus.Add(item); } } return stus; } /// <summary> /// 查找ClassId為3001的學生 /// </summary> /// <param name="student">學生</param> /// <returns>是否為3001班級的學生</returns> public static bool GetClassId(Student student) { if (student.ClassId==3001) { return true; } return false; } /// <summary> /// 年齡大於20的學生 /// </summary> /// <param name="student"></param> /// <returns></returns> public static bool GetBigAge(Student student) { if (student.Age>20) { return true; } return false; } /// <summary> /// 年齡大於15 並且ClassId為1021 /// </summary> /// <param name="student"></param> /// <returns></returns> public static bool GetStuByClassIdAndAge(Student student) { if (student.Age > 15 && student.ClassId==1021) { return true; } return false; } }
下麵這個是在Main方法中執行查詢學生
//List<Student> stus = StudentManager.students; //Console.WriteLine("姓名---年齡---班級--編號"); //foreach (var item in stus) //{ // Console.WriteLine(item.Name+"---"+item.Age+"---"+item.ClassId+"---"+item.Id); //} List<Student> stus1= StudentManager.FindStudents(StudentManager.GetStuByClassIdAndAge); Console.WriteLine("姓名---年齡---班級--編號"); foreach (var item in stus1) { Console.WriteLine(item.Name + "---" + item.Age + "---" + item.ClassId + "---" + item.Id); }