I've been doing some testing and came across something strange. Say I have this interface
interface IRobot
{
int Fuel { get; }
}
As you can see, it's read only. So now i'm going to make a class that implements it
class FighterBot : IRobot
{
public int Fuel { get; set; }
}
Now you can read it and set it. So let's do some tests:
FighterBot fighterBot;
IRobot robot;
IRobot robot2;
int Fuel;
public Form1()
{
InitializeComponent();
fighterBot = new FighterBot();
robot = new FighterBot();
}
First I did this:
Fuel = fighterBot.Fuel;// Can get it
fighterBot.Fuel = 10; //Can set it
That's to be expected, then I did this:
Fuel = robot.Fuel; //Can get it
robot.Fuel = 10; //Doesn't work, is read only
Also to be expected. But when I do this:
robot2 = robot as FighterBot;
Fuel = robot2.Fuel; //Can get it
robot2.Fuel = 10;//Doesn't work, is read only
Why doesn't it work? Isn't it treating the robot2 AS a FighterBot? Therefore, shouldn't it be able to set the Fuel?