Guidelines on clean up code

Many a times, we use the catch block inside the try catch block for our clean up code.

Something like



 // Do something




    // work failed, clean up code here


Rather than the above approach of using the catch block, it would be nicer to use the finally block, something like


bool workSuccessful = false;



   // do some work

   workSuccessful = true;






    // cleanup code here.




There is elegance in the latter method and I would certainly recommend that approach, if you cannot use “using“. See below for details.


PS: Use this approach only if better alternatives are not available. One of the automatic cleanup approaches available with C# is the using construct.

Something like,

using (TextReader tr = new StreamReader(“FileName”))


  // do my work here.



The “using” construct automatically clean up the unmanaged resource (TextReader) once the block has completed execution.

In the event that you cannot use “using“, the try-finally approach would be the best way.


When is a static constructor in C# called?

A static constructor is invoked by the first of either of the following conditions:

  • Create an instance of the class.
  • Refer any of the static methods of the class.

Confused ? Read ahead…


class Sample


   static Sample()


      Console.WriteLine(“static constructor called”);


   public static void WriteTime()


            Console.WriteLine(“Static method called”);


   public static void Main(string[] args)


      Sample.WriteTime(); // call 1

      Sample aNewSample = new Sample(); // call 2



In the above example, call to the static method WriteTime first calls the static contructor of the class Sample and then the static method is called.

In your Console.Window , you will see:

Line1: static constructor called

Line2: Static method called


If after this, you create an instance of the class, the static contructor is not called, as it is only invoked once per class at its first reference (mentioned above). So, call 2

Sample aNewSample = new Sample(); // call 2

will not invoke the static constructor.


On the other hand, if there was no call 1 (call 1 is commented out), the static contructor will be invoked when the first object of Sample class is created (a little before the object is created).

Office live site goes live

Office Live (still in beta) website went live today.

So what does Office live have for me?

Three categories of services:

1. Microsoft Office Live Basic: Easy to use web site design tool (ala Frontpage online), Free domain name and hosting , 5 personalized email accounts(Office live mail), good data storage and data transfer facilities, and best of all web site traffic analysis and reporting tools, with online support. And all this for free, till the beta period.

Web hosting details:

  • Free hosting
  • 30 MB of Web site storage
  • 10 GB of bandwidth
  • No setup or hidden fees
  • Online customer support
  • Daily backup (data retained for 30 days)

    2. Microsoft Office Live Collaboration: Shared sites(remember Sharepoint), on demand company applications (like shared calenders, Employee directory, expenses, jobs, hiring, Customer management, sales and marketing), and 50 MB of disk space, backed by enhanced data protection.

    3. Microsoft Office Live Essentials: Microsoft Office Live Basic + Sharepoint + 50 email accounts + Microsoft project.. — Complete tools to manage your business online… One stop solution for all your needs. (Free domain name and hosting, Easy to use web site design tool (ala Frontpage online).


    So lets get started.

  • Application which does not create an entry in Add/Remove Programs

    Do you want your application not in appear in Add/Remove Programs list?

    If yes, ARPSYSTEMCOMPONENT property of the Windows Installer SDK helps.

    Setting this property to 1 prevents the installed application to be displayed in the Add/Remove Programs List.

    More information on this key is available at

    On a side note, i fyou want a quick access to Add/Remove Programs List, type appwiz.cpl at your Start > Run prompt.