Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code equivalent to += assignment to an event

I was wondering if anyone could tell me the raw code equivalent to the += operator for adding a method to an event. I am curious to how it it works from a technical standpoint.

like image 716
Matt Avatar asked Aug 03 '09 21:08

Matt


People also ask

What is the difference between delegate and events?

Delegate is a function pointer. It holds the reference of one or more methods at runtime. Delegate is independent and not dependent on events. An event is dependent on a delegate and cannot be created without delegates.

What is event delegation in HTML?

Event delegation refers to the process of using event propagation (bubbling) to handle events at a higher level in the DOM than the element on which the event originated. It allows us to attach a single event listener for elements that exist now or in the future. Inside the Event Handling Function.

Can we declare events in interface C#?

An interface can declare an event. The following example shows how to implement interface events in a class. Basically the rules are the same as when you implement any interface method or property.


1 Answers

An event defines a set of methods including "add" and "remove" (in the same way that a property defines "get" and "set"). to this is effectively:

obj.add_SomeEvent(handler);

Internally, the event could do anything; there are 2 common cases:

  • events with a delegate field (including "field-like" events)
  • EventHandlerList implementations

With a delegate, it effectively uses Delegate.Combine:

handler = Delegate.Combine(handler, value);

With an EventHandlerList there is a key object:

Events.AddHandler(EventKey, value);
like image 92
Marc Gravell Avatar answered Sep 30 '22 07:09

Marc Gravell