让类产生事件

今天想了解如何让类产生事件,从http://www.cnblogs.com/Jingu-st/archive/2006/03/23/356677.html中整理出如下的代码:
using System; 

//事先申明一个委托,也可以在类的内部声明委托类型 
public delegate void GameOver();

public class Game

 
// 声明一个事件并指定它的委托类型 
 public event GameOver RaiseGameOver;

 
public void Run()
 
{
  Console.Write(
"请输入一个数:");
  
if (int.Parse(Console.ReadLine())>10)
    
//定义一个触发事件的方法(后面将用到)
    RaiseGameOver();
 }

}
 

public class m

 
public static void Main() 
 

  Game oGame
=new Game();

  
//通过委托,main_gameover 方法 注册到事件中 
  oGame.RaiseGameOver+=new GameOver(main_gameover);
  oGame.Run();
 }


 
public static void main_gameover()
 
//方法要符合委托的类型
 {
  Console.WriteLine(
"Game Over");
 }

}

原文地址:https://www.cnblogs.com/yzx99/p/1244709.html