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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:32:56+00:00 2026-05-26T14:32:56+00:00

I want to be able to call a particular method automatically upon construction of

  • 0

I want to be able to call a particular method automatically upon construction of a derived object, however I can’t think how to do it. The following code illustrates. Another answer recommended OnLoad, but I am doing this for Unity on Mac and OnLoad doesn’t appear to be supported by my platform. Any suggestions?

public class Parent {

    public Parent ()
    {
        // A. Stuff to do before child constructor code runs
        DoThisAutomaticallyAfterConstruction();
    }

    public void DoThisAutomaticallyAfterConstruction()
    {
        // C. In this example, this will run after A, before B. I want it to run ABC
    }
}

public class Child : Parent {

    public Child () : base()
    {
        // B. Stuff to do here after parent constructor code runs
    }
}
  • 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-26T14:32:57+00:00Added an answer on May 26, 2026 at 2:32 pm

    Unfortunately there’s no built-in way to do what you want (it’s a fairly-often-requested feature).

    One workaround is to implement a factory pattern, where you don’t create objects by calling the constructor directly, but instead implement a static method to create them for you. For example:

    public class MyClass
    {
      public MyClass()
      {
        // Don't call virtual methods here!
      }
    
      public virtual void Initialize()
      {
        // Do stuff -- but may be overridden by derived classes!
      }
    }
    

    then add:

    public static MyClass Create()
    {
      var result = new MyClass();
    
      // Safe to call a virtual method here
      result.Initialize();
    
      // Now you can do any other post-constructor stuff
    
      return result;
    }
    

    and instead of doing

    var test = new MyClass();
    

    you can do

    var test = MyClass.Create();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary I want to be able to call a JavaScript function from a Flex
I want to be able to make an HTTP call updating some select boxes
I want to be able to call a global function from an imported class,
I want to be able to call VB scripts from C#, which is easy
I want to be able to call methods on CLR classes from C++. In
I want to be able to expand or collapse all children of a particular
If I have the following code (EDIT: Sorry if I wasn't clear, I want
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects
I want to be able to capture the exception that is thrown when a

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.