public abstract class Shape
{
public String toString()
{
// ...
return "";
}
}
public class Rectangle : Shape
{
public Double width { get; set; }
public Double height { get; set; }
}
Suppose that I created an object from Rectangle class. Are there any way to write properties of Rectangle class object with values via created object without overriding toString() method?
Edit:
Actually my purpose was creating a generic ToString() method for all child classes.
I modify my code
public abstract class Shape
{
public virtual String ToString(Shape shape)
{
String result = String.Empty;
foreach (var property in shape.GetType().GetProperties())
{
result += property.Name + " : " + property.GetValue(shape, null) + "\n";
}
return result;
}
}
public class Rectangle : Shape, IRectangle
{
public Double width { get; set; }
public Double height { get; set; }
public override String ToString()
{
return base.ToString(this);
}
}
Result:
width : 0
height : 0
But now, I have to override ToString() method for all child classes. I could not find solution for this code duplication