Explain Constructors Using C# Programming Language

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
  1. public class Customer {  
  2.     public string Name;  
  3.     public Customer(string name) {  
  4.         this.Name = name;  
  5.     }  
  6. }  
Constructor Overloading
 
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.
  1. public class Customer {  
  2.     public Customer() {  
  3.         .....  
  4.     }  
  5.     public Customer(string name) {  
  6.         .....  
  7.     }  
  8.     public Customer(int id, string name) {  
  9.         .....  
  10.     }  
  11. }  
Constructor C# Code
 
Let's start coding to constructors in C#.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Constructors {  
  7.     class Customer {  
  8.         public int Id;  
  9.         public string Name;  
  10.         public Customer(string Name, int Id) {  
  11.             this.Name = Name;  
  12.             this.Id = Id;  
  13.         }  
  14.         static void Main(string[] args) {  
  15.             Customer myCustomer = new Customer("xyz", 2002);  
  16.             Console.WriteLine("Name :" + myCustomer.Name);  
  17.             Console.WriteLine("Id :" + myCustomer.Id);  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }   
Save the program
 
Constructor Overloading C# Code
 
Let's start coding to constructors overloading in C# code.
  1. using System;  
  2. namespace Constructor_Overloading {  
  3.     class Student {  
  4.         public int Roll_Num;  
  5.         public string name;  
  6.         public Student() {  
  7.             Console.WriteLine("object is created without parameter !! ");  
  8.         }  
  9.         public Student(int num) {  
  10.             this.Roll_Num = num;  
  11.         }  
  12.         public Student(int num, string name) {  
  13.             this.name = name;  
  14.             this.Roll_Num = num;  
  15.         }  
  16.         public Student(string name, int num = 100) {  
  17.             this.name = name;  
  18.             this.Roll_Num = num;  
  19.         }  
  20.     }  
  21.     class program {  
  22.         static void Main(string[] args) {  
  23.             Student s1 = new Student();  
  24.             Student s2 = new Student(10);  
  25.             Student s3 = new Student("Nitin");  
  26.             Student s4 = new Student("Dev", 56);  
  27.             Console.WriteLine("s1.Roll_number = " + s1.Roll_Num);  
  28.             Console.WriteLine("s2.Roll_number = " + s2.Roll_Num);  
  29.             Console.WriteLine("s3.Roll_number = " + s3.Roll_Num);  
  30.             Console.ReadKey();  
  31.         }  
  32.     }  
  33. }   
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.


Similar Articles