1:委托和事件

Code
1
//事件类
2
public class EventClass
3
{
4
public void display(object sender, System.EventArgs e)
5
{
6
Console.WriteLine("This is the Event Class");
7
Console.ReadLine();
8
}
9
}
10
//调用类
11
class InvokeClass
12
{
13
//声明代理对象,注意参数
14
public delegate void delegateobj(object sender, System.EventArgs e);
15
//声明事件对象
16
private event delegateobj obj;
17
//声明要调用的事件类对象
18
private EventClass ec;
19
20
public InvokeClass()
21
{
22
ec = new EventClass();
23
//添加事件对象到事件队列中,参数为要调用的事件
24
this.obj += new delegateobj(ec.display);
25
}
26
//调用delegate对象触发事件
27
protected void OnObj(System.EventArgs e)
28
{
29
if (this.obj != null)
30
{
31
obj(this, e);
32
}
33
}
34
public void RaiseEvent()
35
{
36
EventArgs e = new EventArgs();
37
OnObj(e);
38
}
39
static void Main(string[] args)
40
{
41
InvokeClass ic = new InvokeClass();
42
Console.WriteLine("Please input a string");
43
string input = Console.ReadLine();
44
if (input.Equals(""))
45
{
46
Console.WriteLine("Sorry,you don't input anything");
47
}
48
else
49
{
50
//触发事件
51
ic.RaiseEvent();
52
}
53
}
54
}
55
2:遍历页面中所有的TextBox,交将值设置成"a"

Code
1
for (int j = 0; j < this.Controls.Count; j++)
2
{
3
foreach (object o in Page.Controls[j].Controls)
4
{
5
6
if (o is TextBox)
7
{
8
TextBox txt = (System.Web.UI.WebControls.TextBox)o;
9
txt.Text = "A";
10
}
11
12
}
13
}
14
3 常用排序算法

Code
1
/**//// <summary>
2
/// /冒泡排序
3
/// </summary>
4
private void BubbleSort()
5
{
6
//冒泡排序
7
int[] list = new int[5]
{ 111, 12, 223, 854, -5655 };//初始化数组
8
int i, j, temp;
9
for (j = 1; j < list.Length; j++)
10
{
11
for (i = 0; i < list.Length - j; i++)
12
{
13
if (list[i] > list[i + 1])
14
{
15
temp = list[i];
16
list[i] = list[i + 1];
17
list[i + 1] = temp;
18
}
19
}
20
21
}
22
23
24
}
25
/**//// <summary>
26
/// 选择排序
27
/// </summary>
28
private void SelectSort()
29
{
30
//选择排序
31
int[] a = new int[5]
{ 111, 12, 223, 854, -5655 };//初始化数组
32
int min, min_k;//定义最小数,和最小数的下标
33
for (int i = 0; i < 5; i++)
34
{
35
min = a[i];//将当前循环的数设置成最小数
36
min_k = i;
37
for (int j = i + 1; j < 5; j++)
38
{
39
40
if (a[j] < min)
41
{
42
min = a[j];
43
min_k = j;
44
int tem = a[min_k];
45
a[min_k] = a[i];
46
a[i] = tem;
47
48
49
}
50
51
52
}
53
54
55
}
56
57
}
58