Monday 29 March 2021

Constructors Part 4 | Static Constructors vs Non Static Constructor |

Static Constructors Vs Non-Static Constructors :

If a constructor is explicitly declared by using a static modifier we call that constructor as static constructor whereas rest of other are non-static constructor only.

Constructors are responsible for initializing fields/variables of a class, static fields are initialized by static constructors and non-static fields are initialized by non-static constructors.

Static constructors are implicitly called whereas non-static constructors must be explicitly called.

Static constructors executes immediately once the execution of a class starts and more over it's the first block of code to run under a class whereas non-static constructors executes only after creating the instance of class as well as each and every time the instance of class is created.

In the life cycle of a class static constructors executes one and only one time whereas non-static constructors executes for zero times if no instances are created and "n" times if "n" instances are created.

 Non-static constructors can be parameterized but static constructors can't have any parameters because  static constructors are implicitly called and more over it's the first block of code to run under class.

Non-static constructors can be overloaded where as static constructors can't be overloaded.

 // C# Program to demonstrate
// how to declare the static
// constructor and non-static
// constructor
using System;
class ABC{
// Static variable
static int s;
// Non-static variable
int ns;
// Declaration of
// static constructor
static ABC()
    Console.WriteLine("It is static constructor");
// Declaration of
// non-static constructor
public ABC()
    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
    ABC obj1 = new ABC();

Output :

It is static constructor
It is non-static constructor

Every class contains an implicit constructor if not defined explicitly and those implicit constructors are defined based on the following criteria :

⟶ Every class except a static class contains an implicit non-static constructor if not defined with an explicit constructors .

⟶ Static constructors are implicitly defined only if that class contains any static fields or else that constructor will not be present at all.

Thursday 25 March 2021

Constructors in C#.NET Part 3 | Why Constructors are Needed in our class | Clcoding

Every class requires a constructor to be present init if we want to create the instance of that class.

Every class contains an implicit constructor if not defined explicitly and with the help of that implicit constructor instance of class can be created.

What is the need of defining a constructor explicitly again ?

Implicit constructor of a class will initialize variables of a class with the same value even if we create multiple instance of that class.

If we define constructor explicitly with parameters then we get a change of initializing the fields or variables of the class with a new value every time we are going to create instance of that class.

When ever we define a class identify whether if the class variables requires some values to execute and if they are required then define a constructor explicitly and pass values thru that constructor , so that every time the instance of the class is created we get a chance of passing new values.

Note : Generally every class requires some values for execution and the values that is required for a class to execute are always sent to that class by using the constructor only.

Tuesday 23 March 2021

Types of Constructors in C#.NET Part 2 | C#.NET Tutorial | Clcoding

Type of Constructors 

1. Default or Parameter Less Constructor

2. Parameterized Constructor

3. Copy Constructor

4. Static Constructor

Default or Parameter Less Constructor : 

 If a constructor method doesn't take any parameters then we call that as default or parameter less. these constructors can be defined by a programmer explicitly or else will be default implicitly provided there is no explicit constructor under the class. 

class Test


public Test ()     //Implicit Constructor




Parameterized Constructor :

If a constructor method is defined with out any parameters we call that as parameterized constructor and these constructor can be defined by the programmers only but never can be defined implicitly.

Copy Constructor :

If we want to create multiple instances with the same values then we use these copy constructors, in a copy constructors the  constructors takes the same class as a parameters to it.

Static Constructor :

If a constructor is explicitly declared by using static modifier we call that as static constructor. All the constructors we have defined till now are non-static or instance constructors.

Class Test


static Test ()    //Static constructor defined explicitly



public Test ()   // Implicit default constructor




If a class contains any static the only implicit static constructors will be present or else we need to define them explicitly whereas non-static constructors will be implicitly defined in every class (except static class) provided we did not define them explicitly.

Static constructor are responsible in initializing static variables and these constructors are never called explicitly they are implicitly called and more over these constructor are first to execute under any class.

Static constructors can't be parameterized so overloading static constructors is not possible. 


Monday 22 March 2021

Constructors in C#.NET Part 1 | C#.NET Tutorial | Clcoding

It's a special method present under a class responsible for initializing the variable of that class.

The name of a constructor method is exactly the same name of the class in which it was present and more over it's a non-value returning method.

Each and every class requires this constructor if we want to create the instance of that class.

class Test


int i ;

Test obj = new Test();  // Valid

⟶ It's the responsibility of a programmer to define a constructor under his class and if he fails to do so, on behalf of the programmer an implicit constructor gets defined in that class by the compiler.

 class Test


int i ; string s; bool b;

public Test ()


i = 0;  // Initializing the variables

s = null;

b = false;



Implicitly defined constructors are parameter less and these constructor are also known as default constructors.

Implicitly defined co0nstructor are public.

We can also defined a constructor under the class and if we define it we can call it as explicit constructor and explicit constructor can be parameter less or parameterized also.

[ < modifiers > ] <Name> ( [ < parameter list > ] )



Defining : Implicit or Explicit

Calling : Explicit

Popular Posts


AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (742) Python Coding Challenge (194) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)


Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses