G
GSL
I need help understanding why the following code causes an
InvalidCastException?
//I created an Interface
public interface IFoo
{
string a {get;}
}
//Then a class that implements the interface
public class Foo: IFoo
{
string a {get "something";}
}
//Then a special class that derives from Foo and adds additional methods
public class MyFoo: Foo
{
string b {get "something else";}
}
Why does the following line of code not work?
IFoo foo = new Foo();
MyFoo myFoo = (MyFoo)foo; // <- InvalidCastException!
Thanks.
InvalidCastException?
//I created an Interface
public interface IFoo
{
string a {get;}
}
//Then a class that implements the interface
public class Foo: IFoo
{
string a {get "something";}
}
//Then a special class that derives from Foo and adds additional methods
public class MyFoo: Foo
{
string b {get "something else";}
}
Why does the following line of code not work?
IFoo foo = new Foo();
MyFoo myFoo = (MyFoo)foo; // <- InvalidCastException!
Thanks.