1
Button.Click += new RoutedEventHandler(_click);

private void _click(object sender EventArgs e) 
{
   //... 
} 

In the code above, we're instantiating the RoutedEventHandler type, which is a delegate, with the Button.Click event. But the event is an abstracted delegate by itself, isn't it? I don't understand the difference between this and just instantiating the RoutedEventHandler to a variable, and then adding variables to the instance's invocation list. Am I making this too hard? How do all of the delegates involved here work?

Edit: so my main concern is just trying to bridge the gap between what I know about delegates and what I know about events. I know an event is a delegate wrapped in another layer of abstraction. So when you assign another delegate to its invocation list using the += operator, you're just assigning a delegate to another delegate, correct? But in the code I wrote above, you're not actually instantiating the RoutedEventHandler class, so I'm confused about how you're actually passing it into the invocation list of the Button.Click event. I also get confused because it seems like everything is actually pointing to something else with delegates and events, and the references get complicated.

Andrew B Schultz
  • 1,512
  • 1
  • 23
  • 41
  • Did you mean something like:- RoutedEventHandler r = new RoutedEventHandler(_click);Button.Click +=r;? – Krishnanunni Jeevan May 17 '12 at 23:22
  • Yea; but I don't think I'm making my question clear. I know how to write an event handler, I'm really just trying to understand events and delegates at a deeper level. There's a gap between what I know about delegates and then what I know about events. I'll edit my question to try to articulate it better. – Andrew B Schultz May 18 '12 at 21:22

3 Answers3

1

You can think of events as wrappers to a collection of delegates (with some syntactic sugar for adding / removing members). Events deal with multicasting the call to multiple delegates, you can add custom logic to allow (or not) a delegate to be added (the same way you can wrap a field in a property and add some logic on the getter / setter for the property). Having an event in a class "advertises" to the world that they can safely add handlers (which are implemented as delegates) to receive said events - and that allows for things such as design-time integration with IDEs such as Visual Studio.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
1

When you use a delegate in the context of an event the compiler will generate both a provide backing field for the delegate and a add/remove public property for subscribers to attach to the event. You could just use the delegate as an event as you describe however you will not be able to limit the subscribers to just += and -=

private EventHandler _backingDelegate;
public event EventHandler Click {
   add {
         _backingDelegate += value;
   } 
   remove {
         _backingDelegate -= value;
   }
}
aqwert
  • 10,559
  • 2
  • 41
  • 61
1

Probably this answer will help you.He has explained it in good detail:-

Events

Community
  • 1
  • 1
Krishnanunni Jeevan
  • 1,719
  • 1
  • 15
  • 24