Curriculum
A constructor is a special method in C# that is used to initialize an object of a class. It is called automatically when an object is created, and its primary purpose is to set the initial values of the object’s properties.
Here is an example of a constructor in C#:
public class Person { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } public Person(string name, int age, string gender) { Name = name; Age = age; Gender = gender; } }
In this example, we have defined a constructor for the Person
class that takes three parameters: name
, age
, and gender
. When an object of the Person
class is created, the constructor will automatically be called and the object’s Name
, Age
, and Gender
properties will be initialized with the values passed to the constructor.
Here is an example of how to create an object of the Person
class using the constructor:
Person person1 = new Person("John", 30, "Male");
In this example, we have created an object of the Person
class named person1
and passed in the values “John”, 30, and “Male” to the constructor. These values will be used to initialize the object’s properties.
Rules for constructors in C#:
this
keyword. This is known as constructor chaining.Here is an example of constructor overloading and constructor chaining:
public class Person { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } public Person(string name, int age, string gender) { Name = name; Age = age; Gender = gender; } public Person(string name) : this(name, 0, "Unknown") { } }
In this example, we have defined two constructors for the Person
class: one that takes three parameters and another that takes only one parameter. The second constructor calls the first constructor using the this
keyword, passing in the name
parameter and default values for age
and gender
.
Here is an example of how to create an object of the Person
class using the second constructor:
Person person2 = new Person("Jane");
In this example, we have created an object of the Person
class named person2
using the second constructor, which only takes the name
parameter. Since the age
and gender
parameters are not specified, their default values will be used.
base
keyword. This is known as base constructor chaining.Here is an example of using the base
keyword to call the constructor of the base class:
public class Student : Person { public int Grade { get; set; } public Student(string name, int age, string gender, int grade) : base(name, age, gender) { Grade = grade; } }
In this example, we have defined a Student
class that inherits from the Person
class. The Student
class has an additional Grade
property. The constructor of the Student
class calls the constructor of the base Person
class using the base
keyword, passing in the name
, age
, and gender
parameters. The Grade
property is initialized with the value passed in to the constructor.
Here is an example of how to create an object of the Student
class:
Student student1 = new Student("Mary", 18, "Female", 12);
In this example, we have created an object of the Student
class named student1
and passed in the values “Mary”, 18, “Female”, and 12 to the constructor. The Student
class constructor calls the base Person
class constructor to initialize the Name
, Age
, and Gender
properties, and then initializes the Grade
property with the value 12.
In summary, a constructor is a special method in C# that is used to initialize an object of a class. It is called automatically when an object is created and its primary purpose is to set the initial values of the object’s properties. The rules for constructors in C# include having the same name as the class, being able to have parameters, generating a default constructor if none is defined, allowing for constructor overloading and chaining, and being able to call the base class constructor using the base
keyword.