1
using System;2
using System.Collections.Generic;3

4
namespace ConsoleApplication35


{6

/**//// <summary>7
/// 事件接受类8
/// </summary>9
class Program10

{ 11
static void Main(string[] args)12

{13
EventSend es = new EventSend();14
es.myEvent+=new EventSend.myDelegate(es_myEvent);15
es.Run();16
}17

18
private static void es_myEvent(object sender,MyEventArgs e)19

{20
//事件处理方法21
Console.WriteLine("\n你触发了一次事件!\n你输入的是:"+ e.InputText);22
}23
}24

/**//// <summary>25
/// 事件发送类26
/// </summary>27
class EventSend28

{29
public delegate void myDelegate(object sender, MyEventArgs e);//定义委托30
public event myDelegate myEvent;//定义委托类型的事件31
public void Run()32

{33
while (true)34

{35
Console.BackgroundColor = ConsoleColor.Red;36
Console.ForegroundColor = ConsoleColor.Yellow;37
Console.WriteLine("=== 退出请选择 n 或 N ===");38
MyEventArgs e1 = new MyEventArgs();39
e1.InputText = Console.ReadLine();40
string str = e1.InputText;41
if (str == "n" || str == "N")42

{43
break;44
}45
myEvent(this, e1);46
}47
}48
}49

50

/**//// <summary>51
/// 事件的内容52
/// </summary>53
class MyEventArgs:EventArgs54

{55
private string inputText;56
public string InputText57

{58
get59

{60
return this.inputText;61
}62
set63

{64
this.inputText = value;65
}66
}67
}68
}69

