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

  • SEARCH
  • Home
  • 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 6910561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:49:51+00:00 2026-05-27T08:49:51+00:00

I have been playing around with delegates, events, and anonymous methods. In doing so

  • 0

I have been playing around with delegates, events, and anonymous methods. In doing so one point became very clear.

Would it not streamline the process of registering any event methods or delegate functions in the constructor?

My tests shows it works and it prevents you from having to declare them after instantiation (as the constructor of the object does it for you).

In fact, the performance is pretty good. Are there any drawbacks to using the “this” keyword to refer to the current object when constructing / instantiating the object?

This seems to make a lot of sense to me as all the events would be wired up on instantiation.

Are there any areas this might be an issue?

Example:

//Constructor
public SayHello() 
{
  _name = "Unnamed";
  _isUpdated = false;

  // Register event handler via lambda (ananymous method shorthand)
  this.NameChanged += (object sender, EventArgs e) => { Console.WriteLine(e.message)); };
}
  • 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-05-27T08:49:51+00:00Added an answer on May 27, 2026 at 8:49 am

    There are a couple of potential problems with this approach. First, on the more general side, one should usually favour using method overrides over subscribing to self-published events for performance reasons. Obviously, this isn’t possible if the event is exposed by an externally sourced based class that exposes an event without a corresponding overridable method. However, subscribing to self-published events should be an approach of last resort, not a default approach.

    The second potential problem is more serious, but it has to do with what the code triggered by the event does, not which object exposes the event. For example, consider the following constructor:

    public Foo(Bar bar)
    {
        bar.SomeEvent += (s, e) => this.DoSomething();
    }
    

    If bar fires SomeEvent on another thread, your Foo instance’s DoSomething method could be called before the instance has been fully initialized. This is a fairly well-documented problem in the Java space (see, for example, http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html), but coverage is much sparser for C#/.NET. http://joeduffyblog.com/2010/06/27/on-partiallyconstructed-objects/ provides some detailed coverage for .NET, but it might be more than you wanted to know…

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

Sidebar

Related Questions

I have been playing around with various methods of making the Visitor pattern in
Have been playing around with linq but there is one thing I cant seem
I have been playing around with methods of calling of calling a method safely
I have been playing around with UBIFS some. One test I wrote was a
i have been playing around with apache SOLR since last 3 weeks and very
I have been playing around JavaScript for a year or so, one thing I
I have been playing around with the postgresql.conf file for a couple days now.
I have been playing around with the EF to see what it can handle.
I have been playing around with the MS Source Server stuff in the MS
I have been playing around a bit with a fairly simple, home-made search engine,

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.