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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:09:44+00:00 2026-06-03T05:09:44+00:00

I have often seen code examples of delegate invocation being done as follows: `

  • 0

I have often seen code examples of delegate invocation being done as follows:

`

    public delegate void DelegateThreadActivity<T, U> (T sender, U e);

    public event DelegateThreadActivity<Thread, System.EventArgs> OnStart = null;
    public event DelegateThreadActivity<Thread, System.EventArgs> OnStop = null;

    // Helper method for invocation.
    public void RaiseOnStart ()
    {
        DelegateThreadActivity<Thread, System.EventArgs> instance = null;

        try
        {
            instance = this.OnStart;
            // OR
            instance = (DelegateThreadActivity) (object) this.OnStart;

            if (instance != null)
            {
                instance(this, System.EventArgs.Empty);
            }
        }
        catch
        {
        }
    }

`

Why use the [instance] object at all? At first I thought it was corporate convention but have seen seasoned developers do this as well. What is the benefit?

  • 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. Editorial Team
    Editorial Team
    2026-06-03T05:09:45+00:00Added an answer on June 3, 2026 at 5:09 am

    This is done because of thread safety, and prevention of exception raising in the case the delegate turns null.

    Consider this code:

    if (this.OnStart != null)
    {
      this.OnStart(this, System.EventArgs.Empty);
    }
    

    Between the execution of the if and the execution of this.OnStart, the OnStart delegate could have been manipulated (possibly changed to null, which would result in an exception).

    In the form you provided in your question, a copy of the delegate is made and used when executing. Any change in the original delegate will not be reflected in the copy, which will prevent an exception from coming up. There is a disadvantage with this though: As any changes in the meantime will not be reflected in the copy, which also includes any non-null state, will result in either calling delegates already removed or not calling delegates recently added to it.

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

Sidebar

Related Questions

I have often seen the spinning gears OpenGL example ( I think originally done
I have often seen this expression in a maintenance code Global[Name] What does this
I have often seen an init() within the constructor of AS3 classes, sometimes even
I have often heard this term being used, but I have never really understood
I have often seen tests where canned inputs are fed into a program, one
In code I've seen, some people often use private variables, e.g. private static int
Often I have seen stored procs used for writing business logic in an application.
I have seen this in a lot of code written using Ruby-on-Rails. It seems
I've seen second one in another's code and I suppose this length comparison have
I have often seen Makefiles that start commands with an @ symbol to suppress

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.