In some ASP.NET examples i see that events are used with delegates like this and sometimes without them like this.
Please explain!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
All events are delegate types (they all inherit from EventHandler that inherits from MulticastDelegate which interits from Delegate). Sometimes (or I would rather say most of the time) there is no need to declare your own custom delegate for an event though. You can use existing delegates as long as they match the signature of your event. With the introduction of EventHandler<T> in .NET Framework 2.0, the need for custom event delegates pretty much disappeared (as long as you follow the event design of the framework). So, doing the following:
// declare an event with a custom delegate type public delegate void MyCustomEventHandler(object sender, EventArgs e); public event MyCustomEventHandler SomeCustomEvent;
…is equivalent of this:
// declare an event with an existing delegate type public event EventHandler SomeCustomEvent;
Should you have some custom EventArgs class, you can instead use the generic EventHandler<T> for your events:
class MyCustomEventArgs : EventArgs
{
// you custom stuff here
}
public event EventHandler<MyCustomEventArgs> SomeCustomEvent;
Method 2
You don’t need to specify a delegate if you’re subscribing to an event created by someone else (your asp.net Page or some asp.net web control). Just provide a method that matches the delegate’s signature.
The person who creates the event must provide the delegate. The subscriber just provides the method.
ASP.NET adds to the confusion as it will “wire up” events for you during compilation if you specify AutoEventWireup="true" within your page’s definition.
Method 3
Events in .NET are implemented with delegates.
From the page of your first link:
An event enables objects in a class to
notify other objects that something
has happened that they should perhaps
react to. Events in [.NET] are based
on a publisher-subscriber model. The
class that implements an event is
called the publisher of that event. A
subscriber class can subscribe to a
published event by registering an
appropriate event handler with the
published event.
…
The Delegate type determines the
signature of the event handlers that
can be registered with an event.
So the publishing class defines a delegate that the subscribing classes must implement. When the event is raised the subscribing class’s methods are invoked through the delegate. And a method that handles an event is called an event handler. Events are properties of the class publishing the event. The keyword event is designed to maintain the publish/subscribe idiom.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0