/*
Example code from
Chapter 18 - Generics
C# 2.0: The Complete Reference, Second Edition
by Herbert Schildt
McGraw-Hill/Osborne ? 2006
*/
using System;
class MyEventArgs : EventArgs {
public int eventnum;
}
delegate void MyEventHandler<T, V>(T source, V args);
class MyEvent {
static int count = 0;
public event MyEventHandler<MyEvent, MyEventArgs> SomeEvent;
public void OnSomeEvent() {
MyEventArgs arg = new MyEventArgs();
if(SomeEvent != null) {
arg.eventnum = count++;
SomeEvent(this, arg);
}
}
}
class X {
public void handler<T, V>(T source, V arg) where V : MyEventArgs {
Console.WriteLine("Event " + arg.eventnum + " received by an X object.");
Console.WriteLine("Source is " + source);
Console.WriteLine();
}
}
class Y {
public void handler<T,V>(T source, V arg) where V : MyEventArgs {
Console.WriteLine("Event " + arg.eventnum + " received by a Y object.");
Console.WriteLine("Source is " + source);
Console.WriteLine();
}
}
class Test {
public static void Main() {
X ob1 = new X();
Y ob2 = new Y();
MyEvent evt = new MyEvent();
evt.SomeEvent += ob1.handler;
evt.SomeEvent += ob2.handler;
evt.OnSomeEvent();
evt.OnSomeEvent();
}
}
|