CSharp - Type Creation Overloading Constructors


A class or struct may overload constructors.

One constructor can call another using this keyword.

using System;
class Product
       public decimal Price;
       public int Year;
       public Product (decimal price) { 
          Price = price; 
       public Product (decimal price, int year) : this (price) { 
          Year = year; 

When one constructor calls another, the called constructor executes first.

You can pass an expression into another constructor as follows:

Product (decimal price, DateTime year) : this (price, year.Year) { 

Default constructors

For classes, the C# compiler automatically generates a parameterless public constructor if you do not define any constructors.

If you define one constructor, the parameterless constructor is no longer automatically added by compiler.