Open In App

C# | Difference between Static Constructors and Non-Static Constructors

Prerequisite: Constructors in C#

Static constructors are used to initialize the static members of the class and are implicitly called before the creation of the first instance of the class. Non-static constructors are used to initialize the non-static members of the class. Below are the differences between the Static Constructors and Non-Static Constructors.




// C# Program to demonstrate
// how to declare the static
// constructor and non-static
// constructor
using System;
  
class Geeks{
    
// Static variable
static int s;
  
// Non-static variable
int ns;
  
// Declaration of
// static constructor
static Geeks()
{
    Console.WriteLine("It is static constructor");
}
  
// Declaration of
// non-static constructor
public Geeks()
{
    Console.WriteLine("It is non-static constructor");
}
  
// Main Method
static void Main(string[] args)
{
  
    // Static constructor will call implicitly
    // as soon as the class start to execute
    // the first block of code to execute
    // will be static constructor
  
    // Calling non-static constructor
    Geeks obj1 = new Geeks();
}
}

Output:



It is static constructor
It is non-static constructor




// C# Program to demonstrate
// the execution of static
// constructor and non-static
// constructor
using System;
  
class Geeks{
  
// Declaration of
// static constructor
static Geeks()
{
    Console.WriteLine("Static constructor");
}
  
// Declaration of
// non-static constructor
public Geeks()
{
    Console.WriteLine("Non-Static constructor");
}
  
// Main Method
static void Main(string[] args)
{
  
    // static constructor will call implicitly
    // as soon as the class start to execute
    // the first block of code to execute
    // inside the class will be static
    // constructor
  
    // calling non-static constructor
    // here we are calling non-static
    // constructor twice as we are
    // creating two objects
    Geeks obj1 = new Geeks();
    Geeks obj2 = new Geeks();
}
}

Output: 

Static constructor
Non-Static constructor
Non-Static constructor




// C# Program to demonstrate
// initialization of fields
// by using static constructor
// and non-static constructor
using System;
  
class Geeks{
      
// Static variable
static int s;
  
// Non-static variable
int ns;
  
// Declaration of
// static constructor
static Geeks()
{
    Console.WriteLine("Static constructor");
}
  
// Declaration of
// non-static constructor
public Geeks()
{
    Console.WriteLine("Non-Static constructor");
}
  
// Main Method
static void Main(string[] args)
{
  
    // Static fields can
    // be accessed directly
    Console.WriteLine("Value of s is: " + s);
  
    // Calling non-static constructor
    Geeks obj1 = new Geeks();
  
    // Printing the value
    // of non-static field
    Console.WriteLine("Value of ns is: " + obj1.ns);
}
}

Output: 



Static constructor
Value of s is: 0
Non-Static constructor
Value of ns is: 0




// C# Program to demonstrate
// the passing of parameters
// to constructor
using System;
  
class Geeks {
  
    // static variable
    static int s;
  
    // non-static variable
    int ns;
  
    // declaration of
    // static constructor
    // and passing parameter
    // to static constructor
    static Geeks(int k)
    {
  
        k = s;
        Console.WriteLine("Static constructor & K = " + k);
    }
  
    // declaration of
    // non-static constructor
    Geeks()
    {
        Console.WriteLine("Non-Static constructor");
    }
  
    // Main Method
    static void Main(string[] args)
    {
    }
}

Runtime Error:

prog.cs(18, 16): error CS0132: `Geeks.Geeks(int)’: The static constructor must be parameterless

 


Article Tags :
C#