委托示例(利用委托对不同类型的对象数组排序)
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace delegateTest
6
{
7
/// <summary>
8
/// 演示利用委托给不同类型的对象排序
9
/// </summary>
10
class Program
11
{
12
delegate bool CompareOp(object lhs,object rhs);//声明委托(注意方法签名的格式是两个object类型参数)
13![](/Images/OutliningIndicators/InBlock.gif)
14
static void Main(string[] args)
15
{
16
Employee[] employees = {
17
new Employee("Bugs Bunny",20000),
18
new Employee("Elmer Fudd",10000),
19
new Employee("Daffy Duck",25000),
20
new Employee("Wiley Coyote",(decimal)1000000.38),
21
new Employee("Foghorn Leghorn",23000),
22
new Employee("Road Runner",50000)
23
};
24![](/Images/OutliningIndicators/InBlock.gif)
25
CompareOp c1 = new CompareOp(Employee.CompareEmploySalary);
26![](/Images/OutliningIndicators/InBlock.gif)
27
BubbleSorter.Sort(employees, c1);//对employees数组,按工资高低排序
28![](/Images/OutliningIndicators/InBlock.gif)
29
for (int i = 0; i < employees.Length; i++)
30
{
31
Console.WriteLine(employees[i].ToString());
32
}
33![](/Images/OutliningIndicators/InBlock.gif)
34
Console.WriteLine("---------------------------------------");
35![](/Images/OutliningIndicators/InBlock.gif)
36
object[] ints = { 3, 5, 2, 8, 6, 0 };
37![](/Images/OutliningIndicators/InBlock.gif)
38
c1 = new CompareOp(CompareInt);
39
BubbleSorter.Sort(ints, c1);//对ints数组,按数值大小排序
40
for (int i = 0; i < ints.Length; i++)
41
{
42
Console.WriteLine(ints[i].ToString());
43
}
44![](/Images/OutliningIndicators/InBlock.gif)
45
Console.WriteLine("---------------------------------------");
46![](/Images/OutliningIndicators/InBlock.gif)
47
Console.ReadLine();
48
}
49![](/Images/OutliningIndicators/InBlock.gif)
50
/// <summary>
51
/// 比较整数的大小
52
/// </summary>
53
/// <param name="x">整数1</param>
54
/// <param name="y">整数2</param>
55
/// <returns>如果第一个数小于第二数,返回true,反之false</returns>
56
static bool CompareInt(object x, object y)
57
{
58
return (int)y>(int)x?true:false;
59
}
60![](/Images/OutliningIndicators/InBlock.gif)
61
/// <summary>
62
/// 冒泡排序类
63
/// </summary>
64
class BubbleSorter
65
{
66
static public void Sort(object[] sortArray, CompareOp gtMethod)
67
{
68
for (int i = 0; i < sortArray.Length; i++)
69
{
70
for (int j = i + 1; j < sortArray.Length; j++)
71
{
72
if (gtMethod(sortArray[j], sortArray[i])) //比较大小,注:不同的object,比较大小的方法不同,比如Employee是按工资高低来比较,int是按数字大小来比较,利用委托的好处就在于不用管具体用哪种方法,具体调用的时候才确定用哪种方法
73
{
74
object temp = sortArray[i];
75
sortArray[i] = sortArray[j];
76
sortArray[j] = temp;
77
}
78
}
79![](/Images/OutliningIndicators/InBlock.gif)
80
}
81
}
82
}
83
}
84![](/Images/OutliningIndicators/InBlock.gif)
85
86
/// <summary>
87
/// 员工实体类
88
/// </summary>
89
class Employee
90
{
91
private string name;
92
private decimal salary;
93![](/Images/OutliningIndicators/InBlock.gif)
94![](/Images/OutliningIndicators/InBlock.gif)
95
/// <summary>
96
/// Employee构造函数
97
/// </summary>
98
/// <param name="name"></param>
99
/// <param name="salary"></param>
100
public Employee(string name, decimal salary)
101
{
102
this.name = name;
103
this.salary = salary;
104
}
105![](/Images/OutliningIndicators/InBlock.gif)
106
/// <summary>
107
/// 覆盖ToString()方法
108
/// </summary>
109
/// <returns></returns>
110
public override string ToString()
111
{
112
return string.Format(name + ",{0:c}", salary);
113
}
114![](/Images/OutliningIndicators/InBlock.gif)
115
/// <summary>
116
/// 按员工工资高低比较大小
117
/// </summary>
118
/// <param name="lhs"></param>
119
/// <param name="rhs"></param>
120
/// <returns></returns>
121
public static bool CompareEmploySalary(object lhs, object rhs)
122
{
123
Employee empLhs = (Employee)lhs;
124
Employee empRhs = (Employee)rhs;
125
return (empRhs.salary > empLhs.salary) ? true : false;
126
}
127
}
128
}
129![](/Images/OutliningIndicators/None.gif)
运行结果:![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
82
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
127
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
128
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
129
![](/Images/OutliningIndicators/None.gif)
Elmer Fudd,¥10,000.00
Bugs Bunny,¥20,000.00
Foghorn Leghorn,¥23,000.00
Daffy Duck,¥25,000.00
Road Runner,¥50,000.00
Wiley Coyote,¥1,000,000.38
---------------------------------------
0
2
3
5
6
8
---------------------------------------
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。