Introduction
A constructor is a method that is called when an instance of a class is created.
Why do we need a constructor the intention is to put an object in an earlier state that is to initialize some of the fields in the class.
Constructor
Here's how we declare a constructor. As you see, we have a class called customer. This class has a constructor as you see here a constructor has the exact same name as the class and that's a requirement. If you change this name in the compiler or the runtime, it will not treat these as a constructor. Also note that constructors, unlike other methods, do not have a return type, not even void here in this method. We can do any kind of initialization that required note that we don't always have to use a constructor but it needs to initialize an object upon creation. That's where we put our code. In this example, we have what we call a parameterless or default so this constructor has no parameters. if you don't define a default or parameterless constructor for your class the C# compiler creates one freight. You're not gonna see it, but it will be in the il or intermediate language code. That will be the result of the compilation, the constructor doesn't do anything it just initializes the fields of the class to their default values what are those default values for any kind of numbers. It sets them to zero or boolean types set them to false or any other kinds of reference types like strings or any other objects sets them to know and for characters sets them to an empty character. Here's another example of a constructor. In this example, we have a parameter here, so we get the name of the customer. We set a name field as you see here not not the user of the disc keyword this is a keyword that references the current object why do we need that here look here we have two identifiers that look very similar we have this name field in the class and it's named parameter in the constructor of this class they both have the same spell but are using different naming conventions this parameter here is using camelcase so the first letter of the first word is lowercase the name field here is using pascal case so the first letter of the first word is uppercase. It's possible that sometimes we make a mistake and in the assignment operator, we put the wrong variable on the wrong side of the equation.
In this example, we are passing his name here. We simply copy that to this name field here. That's the right way to do it, but imagine what it would look like if it were the other way around. It wouldn't make a scene, that's why we used this keyword here to make sure what we have on the left side of the assignment operator is a member of the class in this case the name field here so what we get from the outside which is, in this case, the constructor parameter will be copied to the name field okay.
Constructors
- public class Customer {
- public string Name;
- public Customer(string name) {
- this.Name = name;
- }
- }
With that constructor, when we create a new instance of customer we can supply a name here at this point the constructor will be called. The string that is passed here will be copied to the name field we have a concept called constructor overloading means having a method by the same name but different signatures what do I mean by a method signature a signature is what uniquely identifies a method that is its return types its name and the types and members of its parameters in this example here we have three constructors so we have overloaded the constructor for this class each constructor as you see has a different signature, so the first one takes on parameters the second one takes one parameter of type string the third one takes one integer and 1 string as a parameter.
Note that the names here do not matter but the datatypes for the parameters and the order of them are important. So we cannot have two constructors with the exact same signature. Now you wonder why do we need to overload constructor 'z'. The reason for that is to make initialization of this class easier sometimes we may know only the name so we may use this constructor when creating an instance of this class. Sometimes, we may know both the ID and the name so we use this one or sometimes we may know of these parameters so we may use this constructor here.
- public class Customer {
- public Customer() {
- .....
- }
- public Customer(string name) {
- .....
- }
- public Customer(int id, string name) {
- .....
- }
- }
Constructor C# Code
Let's start coding to constructors in C#.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Constructors {
- class Customer {
- public int Id;
- public string Name;
- public Customer(string Name, int Id) {
- this.Name = Name;
- this.Id = Id;
- }
- static void Main(string[] args) {
- Customer myCustomer = new Customer("xyz", 2002);
- Console.WriteLine("Name :" + myCustomer.Name);
- Console.WriteLine("Id :" + myCustomer.Id);
- Console.ReadLine();
- }
- }
- }
Save the program
Constructor Overloading C# Code
Let's start coding to constructors overloading in C# code.
- using System;
- namespace Constructor_Overloading {
- class Student {
- public int Roll_Num;
- public string name;
- public Student() {
- Console.WriteLine("object is created without parameter !! ");
- }
- public Student(int num) {
- this.Roll_Num = num;
- }
- public Student(int num, string name) {
- this.name = name;
- this.Roll_Num = num;
- }
- public Student(string name, int num = 100) {
- this.name = name;
- this.Roll_Num = num;
- }
- }
- class program {
- static void Main(string[] args) {
- Student s1 = new Student();
- Student s2 = new Student(10);
- Student s3 = new Student("Nitin");
- Student s4 = new Student("Dev", 56);
- Console.WriteLine("s1.Roll_number = " + s1.Roll_Num);
- Console.WriteLine("s2.Roll_number = " + s2.Roll_Num);
- Console.WriteLine("s3.Roll_number = " + s3.Roll_Num);
- Console.ReadKey();
- }
- }
- }
Save the Program
Now run the Constructor C# code.
Click on the start button and debug the program.
Outputs for Constructor
Outputs for Constructor Overloading
I hope you understood how to create a constructor using C# Code in Visual Studio.