C# Static Constructor

In this article I will be explaining Static Constructors with the help of small demo.

We use constructors - to set the value of object's data at the time when it is created.

Let's understand Static Constructor with the help of the following demo-

  1. Create a new Console application project - BankAccountDetails
  2. Add a new Class- AccountDetails.cs
  3. Add field data (as accountBalance, currentInterestRate)in the AccountDetails class -

    //Field Data
    publicdouble accountBalance;
    publicstatic double currentInterestRate;

    currentInterestRate is kept as static data - because it has to be same across for all accounts.
    Static data is allocated once and shared amongst all instance of the class.
  4. Add a constructor to initialize the objects data at the time when it is created.

           public AccountDetails(double balance)
                Console.WriteLine("I am in instance level Constructor");
                accountBalance = balance;


  5. Now where do I initialize currentInterestRate which is static variable?

    Since it's not object dependent - It has to be invoked from the class level rather than from an object reference - Right??

    If the value of CurrentinterestRate is known at compile time - I can initialize it directly as -

    publicstatic double currentInterestRate=11.5;
  6. Say the interest rate is not known at compile time and value would be read from database- In this case we would need to initialize it in some method scope/constructor.

    If we try initializing it in the above constructor shown in point 4 -as below-

    public AccountDetails(double balance)
                accountBalance = balance;
                currentInterestRate=11.5  // Also can be fetched from DB

    But in this approach whenever we create a new object our class level static data is reset with the same value and our whole effort of defining static data would not make much sense.


  7. For this particular scenario - We have a   Static Constructor in place - which is used to initialize static variables.

    //Static Constructor
           static AccountDetails()
                Console.WriteLine("I am in static Constructor");
                currentInterestRate = 11.5; //Can be retrieved from DB in the real scenario

  8. Add a Main method to create instances of the class AccountDetails

    publicstatic void Main()
                AccountDetails Person1 =new AccountDetails(15000);
                Console.WriteLine("Account Balance of Person1 is :{0}, ROI is :{1}", Person1.accountBalance,AccountDetails.currentInterestRate);
                AccountDetails Person2 =new AccountDetails(20000);
                Console.WriteLine("Account Balance of Person1 is :{0}, ROI is :{1}", Person2.accountBalance,AccountDetails.currentInterestRate);

  9. Run the application



We we can see our Static Constructor got executed only once before any instance level constructor got called and our static variable (class level variable)- currentInterestRate- got initialized only once.

So to summarize - Below are the key points to be remembered for Static Constructors.

  • A class can have one and only one Static Constructor.
  • The Static Constructor should not have any access modifier.
  • Static Constructors should not have any parameters.
  • The Static constructor will be executed only one time irrespective of number of objects created
  • Runtime invokes the Static Constructor when it creates an instance of the class or before the first static member being accessed.
  • The Static Constructor executes before any of the instance level constructors.

Hopethis cleared up understanding about Static Constructors.

I have attached the code for this sample application.

Happy Learning!!