A constructor is a special type of function with no return type. Name of constructor should be same as the name of the class. We define a method inside the class and constructor is also defined inside a class. A constructor is called automatically when we create an object of a class. We can’t call a constructor explicitly. Let us see the types of constructor.
Remarks on Constructor
- Constructor is called automatically when we create an object of the class.
- Name of constructor should be same as the name of the class.
- Constructor does not return any value.
- Constructor should have a public access modifier.
Constructor Types
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Static Constructor
- Private Constructor
In this article, I am going to explain only two types of constructors.
Default Constructor
Default constructor does not take any parameter. C# compiler creates a default constructor if we do not define any default constructor inside the class, We can also define the default constructor by writing some codes. I defined a default constructor to assign properties of a class. Let us see with the help of an example.
- public class Adminclass
- {
- string userId = string.Empty;
- string password = string.Empty;
-
- public Adminclass()
- {
- userId = "shr";
- password = "Pass";
- }
- }
In the above example, I defined one class named as Adminclass and one default constructor with the same name as the name of the class. I used default constructor to assign the value to the private properties of the class. This constructor will automatically call when we create an object of this class.
How Default constructor is called.
- Adminclass adminObject = new Adminclass();
Remarks on Default constructor
- Default constructor is created by the compiler if we do not create any constructor inside the class.
- Default constructor does not take any parameter.
- Default constructor is called when we create an object of the class.
Parameterized Constructor
Parameterized Constructor is created by the developer. This constructor takes at least one parameter. I have defined one Parameterized constructor below.
- public class Adminclass
- {
- string userId = string.Empty;
- string password = string.Empty;
-
- public Adminclass(string username,string userPassword)
- {
- userId = username;
- password = userPassword;
- }
- }
In the above example, I defined one class named as Adminclass and one parameterized constructor with the same name as the name of the class. This constructor takes two parameters as you can see in the above example. I used a parameterized constructor to assign values to the private properties of the class. This constructor will automatically call when we create an object of this class. Let us see how we can call this Parameterized constructor.
How Parameterized constructor will be called.
Adminclass adminObject = new Adminclass(“Shreesh”,”mypassword”);
Remarks on Default constructor:
- Parameterized constructor is created by the developer, a compiler does not create any parameterized constructor.
- Parameterized constructor takes at least one parameter.
- Parameterized constructor is called when we create an object of the class.
Remark
If we define any constructor, then the C# Compiler does not define a default constructor. A default constructor is created by the compiler when we do not create any constructor inside the class.