C#中的delegate以及利用List<T>中delegate快速实现排序、查找
一、delegate是什么
Delegate中文翻译为“委托”。Msdn中对Delegate的解释如下:
C#中的委托类似于C或C++中的函数指针。使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。与C或C++中的函数指针不同,委托是面向对象、类型安全的,并且是安全的。
我们可以初步认为delegate就是函数指针,但是还是存在一定的不同。
下面是一个最简单的delegate应用。
class Program { public delegate void CompareDelegate(int a,int b); public static void compare(int m,int n) { Console.WriteLine(m+">"+n+ (m>n).ToString()); } static void Main(string[] args) { CompareDelegate cd = new CompareDelegate(Program.compare); cd(1, 2); Console.ReadLine(); } }
一个稍微复杂一点的例子
class Program { public static void delegateFunc_1() { Console.WriteLine("delegate Function one"); } public static void delegateFunc_2() { Console.WriteLine("delegate Function two"); } public static void delegateFunc_3() { Console.WriteLine("delegate Function three"); } static void Main(string[] args) { var td = new test_delegate(); td.delegateObject += new test_delegate.testDelegate(delegateFunc_1); td.delegateObject += new test_delegate.testDelegate(delegateFunc_2); td.delegateObject += new test_delegate.testDelegate(delegateFunc_3); td.runDelegate(); Console.ReadLine(); } } class test_delegate { public delegate void testDelegate(); public testDelegate delegateObject; public void runDelegate() { if (delegateObject != null) { delegateObject.Invoke(); } } }
可以添加多个delegate
二、delegate和event
实际上,C#中的事件处理其实是一种代理,比如点击事件等
class ButtonClickArgs : EventArgs { public string Clicker; } class Program { public static void OnBtnClicked(object sender, ButtonClickArgs e) { Console.WriteLine("this button is clicked by" + e.Clicker); } static void Main(string[] args) { Button myButton = new Button(); myButton.OnClick += new Button.ClickHander(OnBtnClicked); } } class Button { public delegate void ClickHander(object sender, ButtonClickArgs e); public event ClickHander OnClick; public void Click() { OnClick(this, new ButtonClickArgs() { Clicker = "somebody" }); } }
三、利用delegate对List<T>进行排序等
List<T>排序一般有几种方式。
1、需要排序的类实现IComparable接口,而且必须实现CompareTo方法
class Program { static void Main(string[] args) { List<Info> list = new List<Info>(); list.Add(new Info() { Id = 1, name = "ll" }); list.Add(new Info() { Id = 2, name = "fne" }); list.Add(new Info() { Id = 3, name = "fseg" }); list.Sort(); foreach (Info i in list) { Console.WriteLine("Id " + i.Id + " name " + i.name); } Console.ReadLine(); } } class Info : IComparable { public int Id{ get; set;} public string name { get; set; } public int CompareTo(object obj) { int result; Info info = obj as Info; if (this.Id > info.Id){ result = 1; }else{ result = 0; } return result; } }
2、linq to list进行排序
class Program { static void Main(string[] args) { int[] testList = new int[]{5,6,10,2,44,12,3,15,70,65}; var m = from n in testList where n < 10 orderby n descending select n; foreach (var n in m) { Console.WriteLine(n); } Console.ReadLine(); } }
使用delegate可以快速,实现多维度的排序
/*战宠排序,优先级 出战-> 等级 -> style变异 -> quality品质*/ public void sortPet (List<SPkgPetInfo> pets_before) { pets_before.Sort(delegate(SPkgPetInfo x, SPkgPetInfo y) { if(x.m_uiID == Model_Data.LGC_MAIN_PET_ID){ return -1; } if(y.m_uiID == Model_Data.LGC_MAIN_PET_ID){ return 1; } bool matrix_1 = Model_Matrix.Instance().isMatrixPet(x.m_uiID); bool matrix_2 = Model_Matrix.Instance().isMatrixPet(y.m_uiID); if(matrix_1&&!matrix_2){ return -1; } if(!matrix_1&&matrix_2){ return 1; } if (x.m_uiLevel > y.m_uiLevel) { return -1; } if (x.m_uiLevel < y.m_uiLevel) { return 1; } int style_1 = int.Parse (Info_pet.Instance ().getStyleByType(x.m_uiType)); int style_2 = int.Parse (Info_pet.Instance ().getStyleByType(y.m_uiType)); if(style_1>style_2){ return -1; } if(style_1<style_2){ return 1; } int quality_1 = Info_pet.Instance ().getQualityByType (x.m_uiType); int quality_2 = Info_pet.Instance ().getQualityByType (y.m_uiType); if(quality_1>quality_2){ return -1; } if(quality_1<quality_2){ return 1; } return 0; }); }
引用:
hyddd_谈C#中的DelegateDanny Chen_终于会用c#中的delegate(委托)和event(事件)了
浙公网安备 33010602011771号