委托二--------使用委托实现冒泡排序-创新互联
-------------------------------------------------------------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 array :用于接收任何数组,集合
//Func func :委托 ,传递两个参数,一个bool返回值
public static void Sort(IList array, Func 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();
}
}
}
}创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
分享名称:委托二--------使用委托实现冒泡排序-创新互联
标题网址:http://www.jxjierui.cn/article/cdpdhh.html


咨询
建站咨询
