![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace @delegate
{
public delegate void CatShoutEventHandler(object sender , CatShoutEventArgs e);
public class CatShoutEventArgs : EventArgs
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
class Cat
{
private string _name;
public Cat(string name)
{
this._name = name;
}
public event CatShoutEventHandler CatShout;
public void Shout()
{
Console.WriteLine("喵,我是{0}",this._name);
if (CatShout!=null)
{
CatShoutEventArgs e = new CatShoutEventArgs();
e.Name = this._name;
CatShout(this,e);
}
}
}
class Mouse
{
private string _name;
public Mouse(string name)
{
this._name = name;
}
public void Run(object sender, CatShoutEventArgs e)
{
Console.WriteLine("老猫{0}来啦,{1}快跑!",e.Name, this._name);
}
}
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat("Tom");
Mouse mouse1 = new Mouse("Jack");
Mouse mouse2 = new Mouse("Jerry");
cat.CatShout += new CatShoutEventHandler(mouse1.Run);
cat.CatShout += new CatShoutEventHandler(mouse2.Run);
cat.Shout();
Console.ReadKey();
}
}
}
技术改变命运!