R
Richard Corfield
And if not, why not?
For example:
If I define
List<MyClass> foo = new List<MyClass>();
List<Object> bar = (List<Object>)foo;
then I get a compile error on the second line.
The question is why? MyClass is a subclass of Object by definition.
List can only use and expose methods acting on the type its declared
at. MyClass can do anything that Object can[1], as MyClass is a
subclass of Object, so List<Object> shouldn't be able to do anything
that List<MyClass> can't.
[1] Caveat: Dot-Net allows classes to change behaviour depending on the
declared type of the variable that the caller uses to store them, but
even then I expect the behaviour here will be as expected, so this
caveat doesn't break anything.
Thanks
- Richard
For example:
If I define
List<MyClass> foo = new List<MyClass>();
List<Object> bar = (List<Object>)foo;
then I get a compile error on the second line.
The question is why? MyClass is a subclass of Object by definition.
List can only use and expose methods acting on the type its declared
at. MyClass can do anything that Object can[1], as MyClass is a
subclass of Object, so List<Object> shouldn't be able to do anything
that List<MyClass> can't.
[1] Caveat: Dot-Net allows classes to change behaviour depending on the
declared type of the variable that the caller uses to store them, but
even then I expect the behaviour here will be as expected, so this
caveat doesn't break anything.
Thanks
- Richard