Arindam Sinha Online

June 25, 2010

Rules about Constructors, Destructors, and Finalizers

Filed under: Programming — Arindam Sinha @ 4:26 pm
Tags: , , ,

In this post I will refer to an article where the author listed out some important rules about contructor, destructor and finalizers.

Find the thumbrules below –

Rule #1: Contrsuctors are called in descending order
Rule #2: In C# lexicology, a destructor and a finalizer refer to the same thing
Rule #3: Destructors are called in ascending order
Rule #4: Finalizers are a feature of GC-managed objects only
Rule #5: You cannot determine when finalizers would be called
Rule #6: C++/CLI differs between destructors and finalizers
Rule #7: In C++/CLI and classic C++, you can determine when destructors are called
Rule #8: In C++/CLI, destructors and finalizers are not called together
Rule #9: Beware of virtual functions in constructors
Rule #10: User has not control when Static constructor is invoked

The author of the article has nicely explained the first 9 rules in his article with code snippets. I this should be helpful to all the developers.

The Rule#10 is being added by me and I think the purpose of static constructor should be known to all of us. While using Static constructor we can not decide when this will be executed. Also the following rules are true for static constructor –

  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced 
  • Static Constructor can not be called directly.
  • A static constructor does not take access modifiers or have parameter

A proper example of using static constructor will be logger classes which will be initialized before any instance is created in the application. Following code snippet will explain how the initialization is done automatically.

public class Logger
{
 // Static constructor:
 static Logger()
  {
	System.Console.WriteLine("The static constructor of Logger is invoked.");
  }
 public static void Log()
 {
	System.Console.WriteLine("The Log method is invoked.");
 }
}  

class LogTest
{
 static void Main()
 {
	Logger.Log();
 }
}

After executing the above code, following will be the output.

The static constructor of Logger is invoked.
The Log method is invoked.

This signifies that initialization is done automatically. For rest of the rules please read this article.

References

MSDN

Codeproject

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: