17 May 2016 Sudhanshu Ranjan 1.21k

What is static Constructor? Explain.

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.


It is basically used to initialize the static fields and properties of the class. It can be used mainly where, in our program, we want to do something before any reference for the class is created, like changing the background of the application.

There should at most one Static Constructor in a class and also Static Constructor can't take any parameters.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestApp
    class Program
        static Program()
            //code inside this constructor  
        static void Main(string[] args)



Static constructors have the following properties:

1.The user has no control on when the static constructor is executed in the program.
2.A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
3.Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
4.If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
5.A static constructor does not take access modifiers or have parameters.
6.A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
7.A static constructor cannot be called directly.


You can share at :

About Author
Sudhanshu Kumar Ranjan | Lead Engineer at HCL Technologies | Expertise in Microsoft Technology | Author | Blogger | Programmer
Know More
Connect with him on Facebook | Google+ | LinkedIn | Twitter

Recommended Post

Leave a comment

About Dotnet Cube

It's my blog - Dotnet Cube, where you will learn and get solutions about programming, web designing and development. I started this website to share my knowledge and experience which might help someone.

You can get more details about me and my blog at About Us page.

Subscribe Newsletter

Subscribe to our monthly newsletter to get the latest industry news, Media Group updates and our current specials.

Follow On Us...

Home | About | Advertise | Copyrights | Privacy | Contact | Admin

Copyrights © 2015-2019 dotnetcube.com. All rights reserved