In c#, Constructor is a method that will invoke automatically whenever an instance of class or struct is created. The constructor will have the same name as the class or struct, and it is useful to initialize and set default values for the data members of the new object.
If we create a class without any constructor, the compiler will automatically generate one default constructor for that class. So, there is always one constructor that will exist in every class.
In c#, a class can contain more than one constructor with different types of arguments. The constructors will never return anything, so we don’t need to use any return type, not even void while defining the constructor method in the class.
As discussed, the constructor is a method, and it won’t contain any return type. If you want to create a constructor in c#, you need to create a method with the class name.
Following is the syntax of creating a constructor in the c# programming language.
If you observe the above syntax, we created a class called “User” and a method whose name is same as the class name. Here the method User() will become a constructor of our class.
In c#, we have different types of constructors available; those are
Now we will learn about each constructor in a detailed manner with examples in the c# programming language.
In c#, if we create a constructor without any parameters, we will call it a default constructor. Every instance of the class will be initialized without any parameter values.
Following is the example of defining the default constructor in the c# programming language.
If you observe the above example, we created a class called “User” and the constructor method “User()” without having any parameters. When we create an instance of our class (User), our constructor method will automatically be called.
When you execute the above c# program, you will get the result below.
If you observe the above result, our constructor method has called automatically and initialized the parameter values after creating an instance of our class.
In c#, if we create a constructor with at least one parameter, we will call it a parameterized constructor. Every instance of the class will be initialized with parameter values.
Following is the example of defining the parameterized constructor in the c# programming language.
If you observe the above example, we created a class called “User” and the constructor method “User(string, string)” with parameters. When we create an instance of our class (User) with the required parameters, our constructor method will automatically be called.
When you execute the above c# program, you will get the result below.
If you observe the above result, our constructor method has called automatically and initialized the parameter values after creating an instance of our class with the required parameters.
In the next chapters, you will learn about copy, static, and private constructors in the c# programming language with examples.
In c#, we can overload the constructor by creating another constructor with the same method name but with different parameters.
Following is the example of implementing a constructor overloading in the c# programming language.
If you observe the above example, we created a "User" class. We overloaded a constructor “User()” by creating another constructor “User(string, string)” with the same name but with different parameters.
When you execute the above c# program, you will get the result below.
If you observe the above result, the respective constructor methods will call automatically when we create an instance of our class with or without parameters based on our requirements.
In c#, Constructor Chaining is an approach to invoke one constructor from another constructor. To achieve constructor chaining, we need to use this
keyword after our constructor definition.
Following is the example of implementing a constructor chaining in c# programming language.
If you observe the above example, we created different constructors with different parameters, and we are calling one constructor from another constructor using this
keyword.
When you execute the above c# program, you will get the result below.
If you observe the above result, we call one constructor from another to achieve constructor chaining in the c# programming language.
This is how we can achieve constructor chaining in our applications using the c# programming language.