Have CircleAndBox
inherit from neither class, but rather reference objects of those classes. It will have to redefine the methods from each class. You can add implicit conversions to Circle
and Box
to allow it to be used in contexts where references to those objects are expected.
public class CircleAndBox
{
public Circle Circle { get; private set; }
public Box Box { get; private set; }
public CircleAndBox()
{
Circle = new Circle();
Box = new Box();
}
public CircleAndBox Radius(string radius)
{
Circle.Radius(radius);
return this;
}
public CircleAndBox Width(string width)
{
Box.Width(width);
return this;
}
public static implicit operator Circle(CircleAndBox self)
{
return self == null ? null : self.Circle;
}
public static implicit operator Box(CircleAndBox self)
{
return self == null ? null : self.Box;
}
}
Note that the implicit conversions will not preserve the type of the object, so one should not use this technique to pass a CircleAndBox
to a method taking a Box
and expect the result on the other side to be a CircleAndBox
.
CircleAndBox cb = new CircleAndBox();
// Implicit conversion, b contains a Box object.
Box b = cb;
// Compile-time error CS0030.
cb = (CircleAndBox)b;