C# Interview Question #1 – Different Types of Constructors in C#
Posted by: Pravinkumar Dabade ,
on 10/3/2015,
in
Category C#
Abstract: Different Types of Constructors in C# - Default, Parameterized, Copy, Static and Private. C# Interview Question No. 1
Constructor(s) in a class is/are special methods which get called automatically when an object of a class is created. Constructors are specially used to initialize data members. There are different types of constructors you can write in a class -
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Static Constructor
Default Constructor
When you do not declare any constructor, the class will call its default constructor which has a default public access modifier. The default constructor is a parameter less constructor which will be called by a class object.
Let's see an example of a Default Constructor -
public class Person
{
private int m_PersonID;
private string m_FirstName, m_LastName, m_City;
public Person()
{
m_PersonID = 19929;
m_FirstName = "No First Name";
m_LastName = "No Last Name";
m_City = "No City";
}
}
This default constructor will be executed whenever the class is initialized – Person p = new Person();
Note: If the class is abstract, then the accessibility of the default constructor is protected. Otherwise, the accessibility for the default constructor is public
Parameterized constructors
Now let’s see parameterized constructors. You can also call it as constructor overloading. Default constructors always initialize the objects with the same values. In case you want to initialize the class with different values, you can use Parameterized constructors.
public class Person
{
private int m_PersonID;
private string m_FirstName, m_LastName, m_City;
public Person()
{
m_PersonID = 19929;
m_FirstName = "No First Name";
m_LastName = "No Last Name";
m_City = "No City";
}
public Person(string firstName,string lastName)
{
m_FirstName = firstName;
m_LastName = lastName;
}
}
To invoke the parameterized constructor, use this Person p1 = new Person(“DotNet”, “Curry”);
Copy Constructor
Now let's see an example of Copy Constructor. Copy constructor is the parameterized constructor which takes a parameter of the same type. It allows you to initialize a new object with the existing object values.
public class Person
{
private int m_PersonID;
private string m_FirstName, m_LastName, m_City;
public Person()
{
m_PersonID = 19929;
m_FirstName = "No First Name";
m_LastName = "No Last Name";
m_City = "No City";
}
public Person(string firstName,string lastName)
{
m_FirstName = firstName;
m_LastName = lastName;
}
//copy constructor
public Person(Person person)
{
m_PersonID = person.m_PersonID;
m_FirstName = person.m_FirstName;
m_LastName = person.m_LastName;
m_City = person.m_City;
}
}
Here’s an example:
// Instance constructor.
Person p1 = new Person(1, "DotNet", "Curry", "Pune");
// Copy Constructor
Person p2 = new Person(p1);
Static Constructors
Static constructor is used to initialize the static data members of the class. Static constructor is only called once while creation of the first instance of the class. After that, no instance of a class will call the static constructor. You can also use static constructor to execute some code of the class which must be executed only once.
public class Person
{
static Person()
{
//Static Members
}
}
In inheritance, the calling of the constructor starts from the parent class.
Let's see how to use these constructors -
static void Main(string[] args)
{
Person p1 = new Person();//This will call Default Constructor
Person p2 = new Person("Pravin", "D");//This will call two parameterized Constructor
Person p3 = new Person(p2);//This will call Copy Constructor
}
It is worth mentioning that you can also create a private constructor, which is generally used in classes that contain static members only. If you create a private constructor, you cannot create an instance of a class.
class Person
{
// Private Constructor:
private Person() { }
...
}
I hope you got an idea of the different types of Constructors in C#. This question is frequently asked in interviews, so make sure to revise it before heading for your interview.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar [attherate] gmail[dot]com