using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托与事件处理_c_____
{
//委托最常见的用法就是用作回调执行所需任务的方法
//想创建委托,只需使用关键字new创建一个委托类型的新实例,将方法名称作为参数传递即可
//声明委托实例:public delegate void OpDelegate(...)
public class BubbleSort
{
//例子。对数组冒泡排序,比较方法为委托,在类外定义
public delegate bool Order(object first, object second);
public void Sort(Array table, Order sortHandler)
{
if(sortHandler == null)
throw new ArgumentNullException();
bool nothingSwapped = false;
int pass = 1;
while(nothingSwapped == false)
{
nothingSwapped = true;
for(int index = 0;index < table.Length-pass;++index)
{
//用一个 Order 委托确认排序顺序
if (sortHandler(table.GetValue(index),
table.GetValue(index + 1)) == false)
{
nothingSwapped = false;
object temp = table.GetValue(index);
table.SetValue(table.GetValue(index + 1), index);
table.SetValue(temp, index + 1);
}
}
++pass;
}
}
}
}