Constructors and Its Types in C#
Constructors and Its Types in C#
• A special method of the class that is automatically invoked when an instance of the
class is created is called a constructor. The main use of constructors is to initialize
the private fields of the class while creating an instance for the class. When you
have not created a constructor in the class, the compiler will automatically create a
default constructor of the class. The default constructor initializes all numeric fields
in the class to zero and all string and object fields to null.
Types of Constructors
• Default Constructor
• Parameterized Constructor
• Copy Constructor
• Static Constructor
• Private Constructor
Default Constructor
• A constructor without any parameters is called a default constructor; in
other words, this type of constructor does not take parameters. The
drawback of a default constructor is that every instance of the class will
be initialized to the same values and it is not possible to initialize each
instance of the class with different values. The default constructor
initializes:
• All numeric fields in the class to zero.
• All string and object fields to null.
Example:
using System;
namespace DefaultConstructor
{
class addition
{
int a, b;
public addition() //default contructor
{ Output:
a = 100;
b = 175;
}
Example:
Example[cont.]:
class MainClass
{
static void Main()
{
paraconstructor v = new paraconstructor(100, 175); // Creating object of Parameterized Constructor
Console.WriteLine("parameterized constructor ");
Console.WriteLine("\t");
Console.WriteLine("value of a=" + v.a );
Console.WriteLine("value of b=" + v.b);
Console.Read();
}
}
} Output:
Copy Constructor
• The constructor which creates an object by copying variables from another object is
called a copy constructor. The purpose of a copy constructor is to initialize a new
instance to the values of an existing instance.
Syntax
Output:
Static Constructor
• When a constructor is created using a static keyword, it will be invoked only
once for all of the instances of the class and it is invoked during the creation
of the first instance of the class or the first reference to a static member in
the class. A static constructor is used to initialize static fields of the class and
to write the code that needs to be executed only once.
Some key points of a static constructor are:
class Multiplication
{
static int valueOne = 10;
static int product;
static Multiplication()
{
Console.WriteLine("Static Constructor initialized");
product = valueOne * valueOne;
}
public static void Method()
{ Output:
Console.WriteLine("Value of product = " + product);
}
static void Main(string[] args)
{
Multiplication.Method();
}
}
Private Constructor
• When a constructor is created with a private specifier, it is not possible
for other classes to derive from this class, neither is it possible to create
an instance of this class. They are usually used in classes that contain
static members only.
• Some key points of a private constructor are:
• One use of a private constructor is when we have only static members.
• It provides an implementation of a singleton class pattern
• Once we provide a constructor that is either private or public or any,
the compiler will not add the parameter-less public constructor to the
class.
Example:
using System;
namespace defaultConstructor{
public class Counter
{
private Counter() //private constructor declaration
{
}
public static int currentview;
public static int visitedCount()
{ Output:
return ++ currentview;
}
}
class viewCounterdetails
{
static void Main()
{
Console.WriteLine("Private constructor”);
Console.WriteLine();
Counter.currentview = 500;
Counter.visitedCount();
Console.WriteLine("Now the view count is: {0}", Counter.currentview);
Console.ReadLine();
}
}
}
References:
• https://www.onlinegdb.com/online_csharp_compiler
• https://www.c-sharpcorner.com/UploadFile/0c1bb2/constructors-and-its-types-in-
C-Sharp/
• https://www.geeksforgeeks.org/c-sharp-copy-constructor/
• Youtube
• C-sharp Corner