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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:21:37+00:00 2026-06-17T23:21:37+00:00

I have a static container class that holds a handle to some class A

  • 0

I have a static container class that holds a handle to some class A:

public static class Container
{
    private static A _a;        
    public static void Register(A a) { _a = a; }
    public static void Run() { _a.DoIt(); }
}

Registration of the container A instance is performed in the A constructor:

public class A
{
    public A() { Container.Register(this); }        
    public void DoIt() { Console.WriteLine("Running!"); }
}

Now, let’s say that I register my A instance by calling a method that only contains an A instantiation:

public void Init() { var a = new A(); }

Theoretically, could compilation be optimized to ignore this assignment, or can I be 100% sure that A is always instantiated when I call the Init method?

Example When I run the following code:

Init();
...
Container.Run();

will Container._a always be defined and the output from the DoIt method written to the console?

  • 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-17T23:21:39+00:00Added an answer on June 17, 2026 at 11:21 pm

    The compiler doesn’t in general know if the constructor of A has observable side-effects, so it will always call it. It may not keep the variable ‘a’ around though.

    So, the constructor will be called, but the result may not be assigned to the variable; instead the A object may just be immediately registered for garbage collection if nothing else references it. (In your case, something else DOES reference it – namely, the Container class – so it WON’T be garbage-collected!)

    In your case, the constructor manifestly DOES have side-effects in any case (so it would be a major error for the compiler to optimise away the constructor call).

    In summary:

    • The constructor will always be called.
    • The assignment of the result to the local variable may not be done because the compiler knows that it has no observable side-effects.
    • In your code, something else retains a reference to the constructed object, so it won’t be GCed.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have static class that holds some info public static class SampleDataCache { private
If I have a class that holds a static variable that will contain the
I have a templated container class that internally stores data in a following, static,
I'm working with Progress 10.1c I have a class that contains both static and
I have a class which contains a static collection that can be used across
I have a class (singleton) and it contains a static Dictionary private static Dictionary<string,
I have a web application where the masterPage/template contains some static HTML that never
I have a container class that has a generic parameter which is constrained to
I have a class that creates a List<Action<int>> and holds on to them until
I have a C++03 application with a class that holds a single instance of

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.