469,575 Members | 1,206 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

how can I cast List<MyObject> to ICollection<IMyObject>

Why can't I cast List<MyObject> to ICollection<IMyObject>.

MyObject implements IMyObject, and of course, List implements
ICollection.

Thanks

Apr 14 '06 #1
5 19741
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
Apr 14 '06 #2
Because List<MyObject> is a specific type, and ICollection<MyObject> is a
specific type, and List<MyObject> does not inherit or impment
ICollection<MyObject>. Generics are compiled to strong data types. The
Generic "classes" are not classes at all, but templates used to create
classes at compile time.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Jimp" <Ja***********@hotmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Why can't I cast List<MyObject> to ICollection<IMyObject>.

MyObject implements IMyObject, and of course, List implements
ICollection.

Thanks

Apr 14 '06 #3
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
Because List<MyObject> is a specific type, and ICollection<MyObject> is a
specific type, and List<MyObject> does not inherit or impment
ICollection<MyObject>.


Yes it does - that would be fine. List<MyObject> doesn't implement
ICollection<IMyObject> though - that's the important bit (the type
parameter).

I'm guessing your post was actually a typo, but I thought it would be
worth clarifying quickly :)

--
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
Apr 14 '06 #4
Why can't I cast List<MyObject> to ICollection<IMyObject>.

MyObject implements IMyObject, and of course, List implements
ICollection.


Just because List<MyObject> implements ICollection<MyObject> doesn't
mean it also implements ICollection<IMyObject>.

Read more at
http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 14 '06 #5
Hi Jon,

Yeah, sorry for muddying the water.
--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
Because List<MyObject> is a specific type, and ICollection<MyObject> is a
specific type, and List<MyObject> does not inherit or impment
ICollection<MyObject>.


Yes it does - that would be fine. List<MyObject> doesn't implement
ICollection<IMyObject> though - that's the important bit (the type
parameter).

I'm guessing your post was actually a typo, but I thought it would be
worth clarifying quickly :)

--
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

Apr 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Aaron W. West | last post: by
3 posts views Thread by Mike | last post: by
3 posts views Thread by mra | last post: by
5 posts views Thread by Nick Flandry | last post: by
3 posts views Thread by John Howard | last post: by
9 posts views Thread by Frederick Gotham | last post: by
5 posts views Thread by Frederick Gotham | last post: by
7 posts views Thread by * Tong * | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.