Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0108 this() and constructor

To call its overloaded constructor we can use this to reference it. using System; class Rectangle { public int Width; public int Height; public Rectangle():this(0,0){ } public Rectangle(int w, int h){ Width = w; Height = h; } } class Program { static void Main(string[] args) { Rectangle r = new Rectangle(); Console.WriteLine(r.Width); Rectangle r2 = new Rectangle(2, 3); Console.WriteLine(r.Width); } } The output: 0 0