Difference between Static Constructor and Instance Constructor in C#

CsharpProgrammingServer Side Programming

Static Constructor

A static constructor is a constructor declared using static modifier. It is the first block of code executed in a class. With that, a static constructor executes only once in the life cycle of class.

Instance Constructor

Instance constructor initializes instance data. Instance constructor is called when an object of class is created.

The following example shows the difference between static and instance constructor in C#.


 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Difference {
   class Demo {
      static int val1;
      int val2;
      static Demo() {
         Console.WriteLine("This is Static Constructor");
         val1 = 70;
      public Demo(int val3) {
         Console.WriteLine("This is Instance Constructor");
         val2 = val3;
      private void show() {
         Console.WriteLine("First Value = " + val1);
         Console.WriteLine("Second Value = " + val2);
      static void Main(string[] args) {
         Demo d1 = new Demo(110);
         Demo d2 = new Demo(200);


This is Static Constructor
This is Instance Constructor
This is Instance Constructor
First Value = 70
Second Value = 110
First Value = 70
Second Value = 200
Published on 17-Sep-2018 12:32:58