C#事件的简单应用

Car.cs

public class Car
{
    public event EventHandler<EventArgs> Exploded;

    public void show()
    {
        Exploded(this,EventArgs.Empty);
    }
}

public class CarEventArgs : EventArgs
{
}

 

Program.cs

static void Main(string[] args)
{
    Car myCar = new Car();
    myCar.Exploded += new EventHandler<EventArgs>(myCar_Exploded);

    myCar.show();

    Console.Read();
}

static void myCar_Exploded(object sender, EventArgs e)
{
    MessageBox.Show("今天天气不错噢!");
}

原文地址:https://www.cnblogs.com/timy/p/1660076.html