Constructors In C#, Types Of Constructors With Examples
In our previous article we learned about classes in csharp and now in this post let's understand about a new term constructor. Word looks fancy and appears to be doing something constructive, Yes indeed. Let's find out what is a constructor, why it is used and many more stuffs about it.
In the above snippet shows an Automobile class with field variables numberOfWheels, seatingCapacity. There's a method which has same name as class and is the constructor for this class. Since this constructor has no parameter, therefore it is called default constructor or parameterless constructor. There are other types of constructor as well which we will be discussing shortly, worry not.
Constructor is called whenever an instance of the class is created. So this constructor will be called whenever new Automobile(); is called.
C# itself calls a default constructor in case no constructor has been defined in class. That constructor initialize all the variables with default values as per their data types. However default constructor is intentionally added if developer wants the variables to be initialized with values other than defaults. Let's consider below example of a console application
In the above example, the code represent a console app. It has an Automobile class, and in the Main method, we have created an instance of this class Automobile.
Now as we have mentioned earlier, that constructor is called whenever an instance of a class is created. So in this case, constructor will be called and immediately the value 4 & 5 will be assigned to the variables.
So to validate this, Console.Writeline is written to print the value and when this code will run, the output will be
//Output
4
5
This is because of the default constructor. In our example the variables are public to show them in console, but in real development world, it is advised to use private variables and have public properties instead. Make sure you keep that in mind. Now let's have a look at different types of constructors.
Here Automobile(int wheels, int seats) is a parameterized constructor and it is called when parameters are passed at the time of instance creation. Like here 2 & 3 are passed as wheels and seats variable values, so the constructor will set these values to the class variables numberOfWheels & seatingCapacity. You can write any code inside the constructor, set variables to any value, Console.Write anything inside it, it's all upto you.
If no parameters are passed then default constructor will be called.
Since the post has become too long to read, let's break it up into two and understand the other three types of constructors in another post.
Don't leave, the other three types aren't that boring.
What Is A Constructor?
Constructor is a method inside a csharp class which has same name as the class and is used to initialize the variables inside the class. This means that constructor assigns the default value to the variables and it is called whenever an instance of a class is created. These are too many words to understand but consider the below class snippet to understand how exactly constructor appears.
public class Automobile{ public int numberOfWheels; public int seatingCapacity; public Automobile(){ //This method is constructor of class Automobile } }
Constructor is called whenever an instance of the class is created. So this constructor will be called whenever new Automobile(); is called.
C# itself calls a default constructor in case no constructor has been defined in class. That constructor initialize all the variables with default values as per their data types. However default constructor is intentionally added if developer wants the variables to be initialized with values other than defaults. Let's consider below example of a console application
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CheezyCodeApp { class Automobile { public int numberOfWheels; public int seatingCapacity; public Automobile() { numberOfWheels = 4; seatingCapacity = 5; } } class Program { static void Main(string[] args) { Automobile automobileInstance = new Automobile(); //Constructor will be called Console.WriteLine(automobileInstance.numberOfWheels); Console.WriteLine(automobileInstance.seatingCapacity); Console.ReadLine(); } } }
In the above example, the code represent a console app. It has an Automobile class, and in the Main method, we have created an instance of this class Automobile.
Now as we have mentioned earlier, that constructor is called whenever an instance of a class is created. So in this case, constructor will be called and immediately the value 4 & 5 will be assigned to the variables.
So to validate this, Console.Writeline is written to print the value and when this code will run, the output will be
//Output
4
5
This is because of the default constructor. In our example the variables are public to show them in console, but in real development world, it is advised to use private variables and have public properties instead. Make sure you keep that in mind. Now let's have a look at different types of constructors.
Types Of Constructors
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Static Constructor
- Private Constructor
Default Constructor
The example that is explained above is default constructor and is also called parameter-less constructor as it doesn't have any parameter.
Parameterized Constructor
This is similar to default constructor but with parameters. In this constructor you can pass on parameters of your choice at the time of instance creation and these parameters will be available inside constructor for further usage. Consider the below example
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CheezyCodeApp { class Automobile { public int numberOfWheels; public int seatingCapacity; //This will now only be called if //no parameters are passed at instance creation public Automobile() { numberOfWheels = 5; seatingCapacity = 6; } //This will only be called if //parameters are passed at instance creation public Automobile(int wheels, int seats) { numberOfWheels = wheels; seatingCapacity = seats; } } class Program { static void Main(string[] args) { Automobile automobileInstance = new Automobile(2, 3); Console.WriteLine(automobileInstance.numberOfWheels); Console.WriteLine(automobileInstance.seatingCapacity); Console.ReadLine(); } } }
Here Automobile(int wheels, int seats) is a parameterized constructor and it is called when parameters are passed at the time of instance creation. Like here 2 & 3 are passed as wheels and seats variable values, so the constructor will set these values to the class variables numberOfWheels & seatingCapacity. You can write any code inside the constructor, set variables to any value, Console.Write anything inside it, it's all upto you.
If no parameters are passed then default constructor will be called.
Since the post has become too long to read, let's break it up into two and understand the other three types of constructors in another post.
Don't leave, the other three types aren't that boring.
Comments
Post a Comment
Hey there, liked our post. Let us know.
Please don't put promotional links. It doesn't look nice :)