Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 71079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:49:53+00:00 2026-05-10T19:49:53+00:00

I’ve seen some very good questions on Stack Overflow concerning delegates, events, and the

  • 0

I’ve seen some very good questions on Stack Overflow concerning delegates, events, and the .NET implementation of these two features. One question in particular, ‘How do C# Events work behind the scenes?‘, produced a great answer that explains some subtle points very well.

The answer to the above question makes this point:

When you declare a field-like event … the compiler generates the methods and a private field (of the same type as the delegate). Within the class, when you refer to ElementAddedEvent you’re referring to the field. Outside the class, you’re referring to the field

An MSDN article linked from the same question (‘Field-like events‘) adds:

The notion of raising an event is precisely equivalent to invoking the delegate represented by the event — thus, there are no special language constructs for raising events.

Wanting to examine further, I built a test project in order to view the IL that an event and a delegate are compiled to:

public class TestClass {     public EventHandler handler;     public event EventHandler FooEvent;      public TestClass()     { } } 

I expected the delegate field handler and the event FooEvent to compile to roughly the same IL code, with some additional methods to wrap access to the compiler-generated FooEvent field. But the IL generated wasn’t quite what I expected:

.class public auto ansi beforefieldinit TestClass     extends [mscorlib]System.Object {     .event [mscorlib]System.EventHandler FooEvent     {         .addon instance void TestClass::add_FooEvent(class [mscorlib]System.EventHandler)         .removeon instance void TestClass::remove_FooEvent(class [mscorlib]System.EventHandler)     }      .method public hidebysig specialname rtspecialname instance void .ctor() cil managed     {         // Constructor IL hidden     }      .field private class [mscorlib]System.EventHandler FooEvent     .field public class [mscorlib]System.EventHandler handler } 

Since events are nothing more than delegates with compiler-generated add and remove methods, I didn’t expect to see events treated as anything more than that in IL. But the add and remove methods are defined in a section that begins .event, not .method as normal methods are.

My ultimate questions are: if events are implemented simply as delegates with accessor methods, what is the point of having a .event IL section? Couldn’t they be implemented in IL without this by using .method sections? Is .event equivalent to .method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:49:54+00:00Added an answer on May 10, 2026 at 7:49 pm

    I’m not sure that is surprising… compare to the same for properties vs fields (since properties before the same function as events: encapsulation via accessors):

    .field public string Foo // public field .property instance string Bar // public property {     .get instance string MyType::get_Bar()     .set instance void MyType::set_Bar(string) } 

    Also – events do not mention anything about fields; they only define the accessors (add/remove). The delegate backer is an implementation detail; it just so happens that field-like-events declare a field as a backing member – in the same way that auto-implemented-properties declare a field as a backing member. Other implementations are possible (and very common, especially in forms etc).

    Other common implementations:

    Sparse-events (Controls, etc) – EventHandlerList (or similar):

    // only one instance field no matter how many events; // very useful if we expect most events to be unsubscribed private EventHandlerList events = new EventHandlerList(); protected EventHandlerList Events {     get { return events; } // usually lazy }  // this code repeated per event private static readonly object FooEvent = new object(); public event EventHandler Foo {     add { Events.AddHandler(FooEvent, value); }     remove { Events.RemoveHandler(FooEvent, value); } } protected virtual void OnFoo() {     EventHandler handler = Events[FooEvent] as EventHandler;     if (handler != null) handler(this, EventArgs.Empty); } 

    (the above is pretty-much the backbone of win-forms events)

    Facade (although this confuses the ‘sender’ a little; some intermediary code is often helpful):

    private Bar wrappedObject; // via ctor public event EventHandler SomeEvent {     add { wrappedObject.SomeOtherEvent += value; }     remove { wrappedObject.SomeOtherEvent -= value; } } 

    (the above can also be used to effectively rename an event)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a perfectly acceptable practice. These are called Factored… May 12, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer Use $1 instead of \1 For the /g global replace… May 12, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer The EOF flag is only triggered after you attempt to… May 12, 2026 at 12:16 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.