Introduction
Constructor is a special method presented under a class responsible for initializing the variable of that class.
Few points about constructor
- The name of a constructor method is exactly the same name as the class in which it was present
- Constructor never returns any value, so we don’t have to define a return type for it.
- Generally, two types of constructors are implicit constructor and explicit constructor.
- Implicit constructor: It is defined by the complier if the programmer fails to define it. It is a parameter less constructor.
- Explicit constructor: It is defined by the programmer. It can be parameter less or parameterized.
- We call constructor by creating an instance in our main method.
Types of Constructors
- Default or parameter less Constructor.
- Parameterized Constructor.
- Copy Constructor.
- Static Constructor.
- Private Constructor:
Default/parameter less Constructor
If a constructor method doesn’t take any parameters then we call that as default or parameter less.
- It is automatically provided by the compiler if no constructor is defined.
public class MyClass
{
// Default Constructor
public MyClass()
{
// Initialization code
}
}
Parameterized Constructor
If a constructor method takes any parameter then it is called a parameterized constructor. These constructors can be defined by the programmers only but never can be define by implicitly.
- It allows you to initialize the object with specified values during creation.
public class Car
{
// Parameterized Constructor
public Car(string make, string model, int year)
{
Make = make;
Model = model;
Year = year;
}
}
Copy Constructor
If we want to create multiple instances with the same values then we use these copy constructors, in a copy constructor the constructor takes the same class as a parameter to it.
- C# doesn't have a direct syntax for copy constructors, but you can achieve similar functionality using methods or other techniques.
class technicalscripter {
// variables
private string topic_name;
private int article_no;
// parameterized constructor
public technicalscripter(string topic_name, int article_no)
{
this.topic_name = topic_name;
this.article_no = article_no;
}
// copy constructor
public technicalscripter(technicalscripter tech)
{
topic_name = tech.topic_name;
article_no = tech.article_no;
}
// getting the topic name and
// number of articles published
public string Data
{
get
{
return "The name of topic is: " + topic_name +
" and number of published article is: " +
article_no.ToString();
}
}
}
Static Constructor
If a constructor is explicitly declared by using static modifier, we call that as Static Constructor. All the constructor we have define till now are non-static. Static constructors are implicitly call no explicit call is required.
public class MyClass
{
// Static Constructor
static MyClass()
{
// Initialization code for static members
}
}
Private Constructor
- A constructor with private access modifier is called a private constructor.
- It is often used in classes that contain only static members or as part of design patterns.
public class Singleton
{
private static Singleton instance;
// Private Constructor
private Singleton()
{
// Initialization code
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
Thank you and Happy Learning.