Jimp <Ja***********@hotmail.com> wrote:
Why can't I cast List<MyObject> to ICollection<IMyObject>.
MyObject implements IMyObject, and of course, List implements
ICollection.
Because C# generics don't support contravariance/covariance. Even if
they did, you'd need to cast to ICollection<+IMyObject>
(or ICollection<-IMyObject> - I can't remember which way round it is)
rather than ICollection<IMyObject>.
Suppose you *could* cast it. Let's keep things simple and keep it as
casting some IList<string> to IList<object>. It's fine when it comes to
taking elements *out* of the list. It's not so good if you try to add
elements to the list.
Suppose I could do:
IList<string> strings = new List<string>();
IList<object> objects = strings;
// No problem here
object firstObject = objects[0];
// What would this do?
objects.Add (Encoding.UTF8);
It would be trying to add an Encoding to a list of Strings! That would
*have* to fail at runtime, in order to avoid completely violating type
safety. At that stage, you've lost a major point of using generics in
the first place - type safety.
Now, if C# supported covariant/contravariant generics, you *could* do:
IList<string> strings = new List<string>();
IList<+object> objects = strings;
IList<+object> would mean "it's a list of 'something that derives from
object but I don't know what'". (Again, I may have got + and - the
wrong way round, I'm afraid.) That would mean you'd be able to fetch
from the list as object, but not add anything, because you wouldn't
know exactly which type you could add.
Similarly you could do:
IList<object> objects = new List<object>();
IList<-string> strings = objects;
You could then *add* any string to strings, but you couldn't fetch
anything out of it because you wouldn't know what type it was.
I may well have misunderstood all this (in terms of what the CLI
allows) - I've only seen it mentioned in a few places. Anyway, I hope
you can see why C# in its current form, with no added
covariance/contravariance, stops you from doing the cast you want.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too