I have a shape class with square and triangle as inheriting classes
Square square = new Square();
square.SideCount = 4;
//Logic
square.SideCount = 3;
if(square is Triangle)
{
//...
If I instantiate a new square with SideCount = 4, then if I change it’s SideCount to 3, can I put code into the SideCount’s Setter to convert Square to Triangle if sides are set to 3?
Not directly no, an instance of
Squarecannot be changed to an instance ofTriangle.You could have a method, which returns an instance of
Shapedefined on the base class Shape:usage