C # static garbage collector?

I have a simple class that has a static constructor and an instance constructor. Now that I have initialized the class, both static and instance constructors are called. Only static are mentioned once in the application domain. Can I call the same class initialization again and initialize the static constructor? I tried, but it didn’t happen? Is there a way that we can call the static constructor again in the main () method after using the garbage collection in the class.

Here is the code:

public class Employee { public Employee() { Console.WriteLine("Instance constructor called"); } static Employee() { Console.WriteLine("Static constructor called"); } ~Employee() { //Dispose(); } } 

Now in the main method call:

 static void Main(string[] args) { Employee emp = new Employee(); Employee emp = new Employee(); } 

Conclusion:

Static constructor called Instance constructor Instance constructor called

Now static did not call again. Because it is called once in the application domain. But this is their way, which we could call again, without unloading the application area. Can we use the GC class here?

Thanks. Pal

+4
source share
3 answers

If you don't produce it with reflection, the static constructor (or, in a more general sense, a type initializer) will only run once for a particular class, for AppDomain.

Note that for generics, using different type arguments, you will get different specific classes:

 public class Foo<T> { Foo() { Console.WriteLine("T={0}", typeof(T)); } public static void DummyMethod() {} } ... Foo<int>.DummyMethod(); // Executes static constructor first Foo<string>.DummyMethod(); // Executes static constructor first Foo<string>.DummyMethod(); // Type is already initialized; no more output 
+8
source

Impossible. The CLR stores an internal state bit that keeps track of whether the type initializer has been started. He cannot work again. This status bit is actually stored in the loader heap as part of the AppDomain state. The workaround is simple, just add a static method to the class.

+2
source

The constructor point should put things in the desired initial valid state.

The instance constructor places the instance in its initial valid state.

An instance constructor that accepts arguments places the instance in its initial valid state, which reflects its arguments.

The static constructor places the type in its initial valid state. For instance. initialization of static elements used by static methods of the class or shared by all instances.

Ideally, all methods will leave the object and type in an acceptable state, but the designers differ in that they are responsible for the fact that they fell into it in the first place.

Any attempt to call the constructor twice is an error, since “re-entering it in the initial correct state” is not something you can logically do twice (“initial” and “again” do not work well in the same sentence). The compiler helps us (refusal to compile in it) and the language (in the absence of a way to express this) from doing such a thing.

And, being a logical impossibility, this is not something you can really do (well, I want to draw a triangle with more than three sides, but only to say that I did it). This suggests that you are using your constructor to do something other than configure the initial valid state.

Doing anything other than establishing such the correct state in the constructor is (as it doesn’t work) an optimization at best, often a serious design flaw, and possibly (worst of all, because it lasts longer), an optimization attempt that really a serious design flaw.

One of the signs that your optimization attempt is actually a design flaw is the desire to call the static constructor more than once or call the instance constructor more than once on the same object.

Define the desired repeated behavior, move it to a separate method, and call it as necessary, both from the constructor and from other sources. Then double-check your design logic, as this is a pretty serious mistake to search for in class design and suggests that you have deeper problems.

+1
source

All Articles