A simple question...
I have an abstract class Cell and two classes BorderCell and BoardCell, which inherit from the Cell class. Then I have a Cells array with the type of Cell[], which contains objects of the type BorderCell and BoardCell.
abstract class Cell
{
}
class BorderCell : Cell
{
public void Method1(){};
}
class BoardCell: Cell
{
public void Method2(){};
}
...
Cell[] Cells = new Cell[x];
for (int i = 0; i < x; i++){
Cells[i] = new BorderCell();
// or
Cells[i] = new BoardCell();
}
Now I want to cast a cell to BorderCell and run its Method1, like this:
(Border)Cells[i].Method1();
But this doesn't work, I have to use:
BorderCell borderCell = (BorderCell)Cells[i];
borderCell.Method1();
Is this the only (and the right way) to do this)?