What is a constructor ?
A constructor in c# is a special method whose name is same as class name and which is not having any return type.
A Constructor in C# is used for initializing the object.
A Constructor in C# is used for filling data into the object.
All the above definitions are valid.
A Constructor in C# is used for filling or initializing data into objects .
Sample : Assume you have a class with the name Doctor and now you want to create an object and fill below given 2 doctors details into two 2 objects .
First Doctor details : name --> Dr.Rajeev ; age --> 39 ; qualification --> MBBS
Second Doctor details : name --> Dr.Suresh ; age --> 67 ; qualification --> MS
public class Doctor
public string name;
public int age;
public string qualification ;
public Doctor (string name, int age ,string qualification )
this.name=name ; this.age =age ; this.qualification=qualification;
public class Test
public static void Main (string args )
Doctor d1=new Doctor ("Dr.Rajeev",39,"M.B.B.S") ; // THIS RIGHT HAND SIDE CODE WILL CONSTRUCTOR AND FILLS FIRTS DOCTOR DATA INTO OBJECT .
Doctor d2=new Doctor ("Dr.Suresh",67,"M.S") ; // THIS RIGHT HAND SIDE CODE WILL CONSTRUCTOR AND FILLS SECOND DOCTOR DATA INTO OBJECT .
Back To Top