I’m reading the msdn library topic about genrics . There is an example of declaring event wiht generic delegates, but is it correct?
// Code block 8. Generic event handling
public delegate void GenericEventHandler<S,A>(S sender,A args);
public class MyPublisher
{
public event GenericEventHandler<MyPublisher,EventArgs> MyEvent;
public void FireEvent()
{
MyEvent(this,EventArgs.Empty);
}
}
public class MySubscriber<A> //Optional: can be a specific type
{
public void SomeMethod(MyPublisher sender,A args)
{...}
}
MyPublisher publisher = new MyPublisher();
MySubscriber<EventArgs> subscriber = new MySubscriber<EventArgs>();
publisher.MyEvent += subscriber.SomeMethod; // is this line correct?
Can we directly apply method to event, without wrapping it firstly with our delegate?
Yes, this is new functionality in C# 2.0 and it will create the delegate for you. Note that you are still creating a delegate, but the creation is invisible.