委托二--------使用委托实现冒泡排序
2014-08-07 21:27
288 查看
-------------------------------------------------------------Proson.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { public class Proson { public string Name { get; private set; } public decimal Money { get; private set; } public Proson(string name, decimal money) { this.Name = name; this.Money = money; } public override string ToString() { return string.Format("姓名:{0};月薪:{1}", Name, Money); } //此方法用于传为委托 public static bool IsMax(Proson p1, Proson p2) { return p1.Money < p2.Money; } } }-------------------------------------------------------------MySort.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { public class MySort { //IList<T> array :用于接收任何数组,集合 //Func<T, T, bool> func :委托 ,传递两个参数,一个bool返回值 public static void Sort<T>(IList<T> array, Func<T, T, bool> func) { bool yesno = true; do { yesno = false; for (int i = 0; i < array.Count - 1; i++) { if (func(array[i], array[i + 1]))//用到了委托【bool IsMax(Proson p1, Proson p2)】 { T temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; yesno = true; } } } while (yesno); } } }-------------------------------------------------------------主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Proson[] p = { new Proson("张翼德", 100000m), new Proson("赵子龙", 50000m), new Proson("关云长", 150000m), new Proson("诸葛亮", 200000m) }; //传递一个数组,和一个方法 MySort.Sort(p, Proson.IsMax); foreach (var item in p) { Console.WriteLine(item.ToString()); Console.ReadLine(); } } } }
相关文章推荐
- c#使用委托实现异步调用
- 如何使用委托与事件来实现观察者模式
- 使用委托实现用户控件与父页面的交互
- 使用委托和事件实现观察者模式(Observer Pattern)
- Windows Phone开发之线程使用委托实现更新UI
- 使用委托在用户自定义控件中实现事件响应
- C# 使用委托实现多线程调用窗体
- 委托让冒泡排序的扩展更加优雅--开闭原则的使用
- C#中使用委托和事件实现观察者模式(observer pattern)
- C#中使用委托创建事件实现两个窗体间通信
- 使用委托在用户自定义控件中实现事件响应
- 使用委托实现用户控件与父页面的交互
- C#中使用委托表达式与匿名方法实现委托方法
- 委托让冒泡排序的扩展更加优雅--开闭原则的使用
- 使用.NET 3.5 Lambda表达式实现委托
- 使用javascript的“委托”实现attachEvent
- 使用委托在用户自定义控件中实现事件响应
- .Net中使用事件和委托实现Observer模式(二)
- 使用委托在用户自定义控件中实现事件响应
- 委托让冒泡排序的扩展更加优雅--开闭原则的使用