//业务逻辑:我有一个防盗器,当我的汽车其他人启动时,警告我汽车被启动了

//Program.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace EventExample
{
        class Program
        {
                static void Main(string[] args)
                {
                        Car cr = new Car();//实例化类
                        cr.carStartEvent += new Car.CarStartDelegate(cr_carStartEvent);//订阅汽车启动事件

                        Console.WriteLine("汽车被别人启动了...");
                        cr.myCarStart();//触发汽车启动的方法

                        Console.Read();
                }

                static void cr_carStartEvent(string startwarming)
                {
                        Console.WriteLine(startwarming);//接收到来自汽车的通知,显示在屏幕上
                }
        }
}


//Car.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace EventExample
{
        class Car
        {
                public delegate void CarStartDelegate(string startwarming);//通过委托通知订阅者

                public event CarStartDelegate carStartEvent;//声明事件

            
                public void myCarStart()
                {
                        carStartEvent("通知:汽车被启动了!");
                }

        }
}