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 53983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:08:56+00:00 2026-05-10T17:08:56+00:00

In C#, I find myself occasionally wanting to register a method for an event

  • 0

In C#, I find myself occasionally wanting to register a method for an event in the middle of a dispatch of that same event. For example, if I have a class that transitions states based on successive dispatches of the same event, I might want the first state’s handler to unregister itself and register the second handler. However, I don’t want the second handler to be dispatched until the next time the event is fired.

The good news is that it looks like the Microsoft implementation of C# behaves exactly this way. The event registration syntax sugar gets replaced with a call to System.Delegate.Combine, which just concatenates the current invocation list and the new method into a separate list and assigns it to the event property. This gives me exactly the behavior I want.

So, my question is: is this guaranteed behavior by the language standard? I like to be able to run my C# code on other platforms under mono and generally want to make sure I’m not making assumptions about the language standard based on its implementation.

I couldn’t find any definitive information on MSDN.

If you’d like a specific example of what I’m talking about, here’s an example:

    delegate void TestDelegate();     static event TestDelegate TestEvent;      static void Main (string[] args) {         TestEvent += TestDelegateInstanceFirst;         TestEvent();         TestEvent();     }      static void TestDelegateInstanceFirst () {         Console.WriteLine('First');         TestEvent += TestDelegateInstanceSecond;     }      static void TestDelegateInstanceSecond () {         Console.WriteLine('Second');     } 

At least on Windows, the output is:

First First Second 
  • 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-10T17:08:56+00:00Added an answer on May 10, 2026 at 5:08 pm

    Yes, it’s guaranteed.

    From the unified C# 3.0 spec, section 15.1:

    However, when two non-null delegate instances are combined, their invocation lists are concatenated—in the order left operand then right operand—to form a new invocation list, which contains two or more entries.

    Note the ‘new invocation list’. And again in section 15.3:

    Once instantiated, delegate instances always refer to the same target object and method. Remember, when two delegates are combined, or one is removed from another, a new delegate results with its own invocation list; the invocation lists of the delegates combined or removed remain unchanged.

    Finally, MSDN for System.Delegate states:

    Delegates are immutable; once created, the invocation list of a delegate does not change.

    I suspect there’s something in the CLI spec – I’ll check if you’d like, but hopefully these three have given you enough confidence 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 79k
  • Answers 79k
  • 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 http://perldoc.perl.org/perlre.html#Combining-RE-Pieces: if we match a regular expression a|ab against "abc"… May 11, 2026 at 4:01 pm
  • Editorial Team
    Editorial Team added an answer Not 100%. Ruby blocks require you to name all your… May 11, 2026 at 4:01 pm
  • Editorial Team
    Editorial Team added an answer My guess is that your issue is in the database… May 11, 2026 at 4:01 pm

Related Questions

Sometimes I find myself in the situation where I want to execute several sequential
When should someone use structs instead of classes or vice versa in C++? I
I was wondering if anyone could point to an Open Source date utility class
I have a naming strategy for denoting the nature of code entity (variable, method,

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.