If I re-write the following code with auto-implemented properties, I understand I have to use a constructor to assign initial values to the variables.
But what happens to the override method? When I remove it, the program does not output anything...
In other words, if I use a constructor and then add auto-implemented properties, I can no longer just use rectangle
to simply print out both width
and height
. Can I?
using System;
namespace Rectangle_Solution
{
public class Rectangle
{
private int width = 5;
private int height = 3;
public int Width
{
get
{
return width;
}
set
{
width = value;
}
}
public int Height
{
get
{
return height;
}
set
{
height = value;
}
}
public override string ToString()
{
return "width = " + Width + ", height = " + Height;
}
public static void Main( string[] args )
{
Rectangle rectangle = new Rectangle();
Console.WriteLine( "rectangle details - {0}", rectangle );
rectangle.width = 10;
rectangle.height += 1;
Console.WriteLine( "rectangle details - {0}", rectangle );
}
}
}