Overloading constructors

Constructors can be overloaded as well.

The Rectangle class listed below has two constructors.


using System;

class Rectangle {
   public int Width;
   public int Height;
   
   public Rectangle(){
      Width  = 0;
      Height = 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.