- Constructor is a method that is called when an instance of a class is created.
- Usage of a constructor is to put an object in an early state (To initialize some of the class fields in a class).
- Constructor can be declared as below.
public class Customer { //Default Constructor
public Customer() { } } - If we don't define a default constructor or parameterless constructor, C# compiler automatically will create it when an instance of a class is created.
public class Customer { public bool IsMarried; public string Age; } class Program { static void Main(string[] args) { var customer = new Customer(); Console.WriteLine(customer.IsMarried); Console.WriteLine(customer.Age); Console.Read(); } } Output False 0
- We can also define a constructor with one or more parameter which is called parameter constructor.
public class Customer {
public string Name;
//Parameter Constructor
public Customer(string name) { this.Name = name; } } - Constructors can be overloaded. Overloading means creating a method with the same name and different signatures. Signature is what uniquely identify a method. That includes return type, name, parameter type , no of input parameters and order of the parameters.
public class Customer {
public Customer(){} public Customer(string name){} public Customer(string name, int Age){} }
- As a best practice, when we have a list of objects inside a class, always the list should be initialized in the constructor or on the declaration. Otherwise it will give an exception.
public class Order { } public class Customer { public string Name; public int Age; public List<O
rders>; public Customer() { } public Customer(string name) { this.Name = name; } public Customer(string name, int age) { this.Name = name; this.Age = age; } } class Program { static void Main(string[] args) { var customer = new Customer(); var order = new Order(); customer.orders.Add(order); Console.Read(); } } - So we should initialize the list of order inside the constructor. But the problem is, when we use other parameter constructors , list will be set to null again. To overcome this, We can pass control from one constructor to the other by using the this keyword.
public class Customer { public string Name; public int Age; public List
orders; public Customer() { orders = new List (); } public Customer(string name) :this() { this.Name = name; } public Customer(string name, int age) : this(name) { this.Name = name; this.Age = age; } }
- But still this way is little ugly and hard to maintain. So you should only define a constructor when you really have to initialize some fields and etc.
Constructors Inheritance
- When creating an object of a type that is part of an inheritance hierarchy, base class constructors are always executed first. They are not inherited to the derived class and need to define explicitly.
public class Vehicle { public Vehicle() { Console.WriteLine("Vehicle is being initialized.."); } } public class Car : Vehicle { public Car() { Console.WriteLine("Car is being initialized.."); } } public class Program { static void Main(string[] args) { var obj = new Car(); Console.Read(); } }
OutPut
Vehicle is being initialized..
Car is being initialized..
public class Vehicle
{
private readonly string _registrationNumber;
public Vehicle(string registrationNumber)
{
this._registrationNumber = registrationNumber;
Console.WriteLine("Vahicle is being initialized.. Registration No : " + registrationNumber);
}
}
public class Car : Vehicle
{
public Car(string registrationNumber) :base(registrationNumber)
{
Console.WriteLine("Car is being initialized.. Registration No : "+ registrationNumber);
}
}
public class Program
{
static void Main(string[] args)
{
var obj = new Car("CAS-5276");
Console.Read();
}
}
OutPut
Vahicle is being initialized.. Registration No : CAS-5276
Car is being initialized.. Registration No : CAS-5276
No comments:
Post a Comment