- public class MyClass : IDisposable
- {
- public event EventHandler Disposing;
- public void Dispose()
- {
- // release any resources here
- if (Disposing != null)
- {
- // someone is subscribed, throw event
- Disposing (this, new EventArgs());
- }
- }
- public static void Main( )
- {
- using (MyClass myClass = new MyClass ())
- {
- // subscribe to event with anonymous delegate
- myClass.Disposing += delegate
- { Console.WriteLine ("Disposing!"); };
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/050620133788.html
来源: http://www.codesnippet.cn/detail/050620133788.html