C#事件示例程序
using System;
using System.Collections.Generic;
using System.Text;

namespace MyCalculate {

    
public delegate void CalculateEventHandler(object sender,EventArgs e);

    
class CalculateWithEvent {
        
public event CalculateEventHandler Calculate;

        
protected virtual void OnCalculate(EventArgs e) {
            
if (Calculate != null{
                Calculate(
this, e);
            }

        }

        
public int Add(int a, int b) {
            OnCalculate(EventArgs.Empty);
            
return a + b;
        }


        
public int Sub(int a, int b) {
            OnCalculate(EventArgs.Empty);
            
return a - b;
        }

    }


    
class EventListener {
        
private CalculateWithEvent CalculateCase;

        
public EventListener(CalculateWithEvent calculateCase){
            CalculateCase 
= calculateCase;
            CalculateCase.Calculate 
+= new CalculateEventHandler(CalculateCase_Calculate);
        }


        
void CalculateCase_Calculate(object sender, EventArgs e)
        
{
            
//throw new Exception("The method or operation is not implemented.");
            Console.WriteLine("运算事件被调用!");
        }

    }

 
}


namespace TestNS
{
    
using MyCalculate;

    
class Test
    
{
        
static void Main(string[] args)
        
{
            CalculateWithEvent myCalculate 
= new CalculateWithEvent();
            EventListener myListener 
= new EventListener(myCalculate);
            myCalculate.Add(
23);
            myCalculate.Sub(
32);
        }

    }

}


虚拟的,用于在基类定义一个方法,以便在继承类中重写方法(override)