根据菜鸟教程的说法:
事件(Event) :基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些出现,如系统生成的通知。应用程序需要在事件发生时响应事件。例如,中断。事件是用于进程间通信。
属性:类或结构中的成员变量或方法称为 域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用 访问器(accessors) 让私有域的值可被读写或操作。
.NET Framework 的EventArgs:表示包含事件数据的类的基类,并提供用于不包含事件数据的事件的值。
- 属性
//C#属性示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CsharpConsoleappTest
{
class Student
{
private int stuid=20105151;
private string major="computer science";
private DateTime signDate=DateTime.Now;
public int Stuid
{
get
{
return stuid;
}
set
{
stuid = value;
}
}
public string Major
{
get
{
return major;
}
set
{
major = value;
}
}
public DateTime SignDate
{
get
{
return signDate;
}
set
{
signDate = value;
}
}
public void printinfo()
{
//signDate设置短输出
Console.WriteLine("学号:{0},专业:{1},入学日期:{2}", stuid, major, signDate.ToString("d"));
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请逐行输入学号,专业,入学日期:");
Student student = new Student();
student.Stuid = Int32.Parse(Console.ReadLine());
student.Major = Console.ReadLine();
student.SignDate = DateTime.Parse(Console.ReadLine());
student.printinfo();
}
}
}
- 委托和事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CsharpConsoleappTest
{
class eventRaiser
{
private char keypress;
public delegate void keyPressHandler(char keyInfo);//声明委托
public event keyPressHandler keyEvent;//声明事件
public event keyPressHandler keyEventplus;
public void onkeyPress()
{
if (keyEvent != null)
{
keyEvent(keypress);
}
}
public void onkeyPressPlus()
{
if(keyEventplus!=null)
{
keyEventplus(keypress);
}
}
public void setKey(ConsoleKeyInfo info)
{
//keychar属性获取对应的unicode字符
keypress = info.KeyChar;
if (keypress == 'a')
onkeyPress();
else if (keypress == 'b')
{
onkeyPressPlus();
}
else
{
Console.WriteLine(" 没有事件被触发");
}
}
}
class eventSubscriber
{
//事件处理程序
public void printKeyInfo(char i)
{
Console.WriteLine(" 按键{0}被按下", i);
}
}
class Program
{
static void Main(string[] args)
{
eventRaiser raise = new eventRaiser();
eventSubscriber subscriber = new eventSubscriber();
raise.keyEvent += subscriber.printKeyInfo;
//将事件处理程序和事件绑定起来
raise.keyEventplus += new eventRaiser.keyPressHandler(subscriber.printKeyInfo);
Console.WriteLine("请按下一个键:");
raise.setKey(Console.ReadKey());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RaisingEvent
{
public class EventSource //编写发行者类
{
public event System.EventHandler EventX; //声明一个事件
public void RaiseEventX() //声明一个引发事件的方法
{
if (EventX != null) //判断事件是否为空
{
EventX (this, System.EventArgs.Empty ); //不为空则使用委托
}
}
}
public class EventSink //编写订阅者类
{
public EventSink(EventSource es) //声明构造函数
{
es.EventX += new System.EventHandler(ReceiveEvent); //将委托与方法关联
}
public void ReceiveEvent(object sender, System.EventArgs e) //声明一个处理事件的方法
{
System.Console.WriteLine("EventX raised");
}
}
class Program
{
static void Main(string[] args)
{
EventSource source = new EventSource(); //创建一个EventSource类的实例
EventSink sink = new EventSink(source); //创建一个EventSink类的实例
source.RaiseEventX(); //调用RaiseEventX()方法
}
}
}