By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

cast List<SubClass> to ICollection<Class>

P: n/a
Hi,

How can I get around runtime error that says I can not explicit cast
List<SubClassto ICollection<Class>?

Generic List inhertis generic ICollection and
Subclass inherits Class,

then should not a problem to cast it explicitly, right?

Thanks!
Nov 13 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Wed, 12 Nov 2008 16:24:01 -0800, Ryan <Ry**@discussions.microsoft.com>
wrote:
Hi,

How can I get around runtime error that says I can not explicit cast
List<SubClassto ICollection<Class>?

Generic List inhertis generic ICollection and
Subclass inherits Class,

then should not a problem to cast it explicitly, right?
It is and should be a problem to cast it. If you could cast it, then you
could then treat your List<SubClasscollection as if it was the
more-general ICollection<Class>. And if you can do that, then you can add
an instance of Class to your object that is really a List<SubClass>. And
if you can do that, then all of the sudden you have no way to guarantee
that everything in your List<SubClassis really an instance of SubClass.

Google (the web or this newsgroup) for the keywords "covariant",
"covariance", and "generic". This is a fairly common question and there
is a lot more in-depth discussion in this newsgroup and elsewhere.

As an interesting side-note: C# 4.0 is supposed to include support for
covariant generics. It still won't apply in this case -- they aren't
allowing covariance anywhere that doing so could break something -- but it
will address this very common question in other scenarios where it
actually _is_ safe to cast something in this way.

Pete
Nov 13 '08 #2

P: n/a


"Peter Duniho" wrote:
On Wed, 12 Nov 2008 16:24:01 -0800, Ryan <Ry**@discussions.microsoft.com>
wrote:
Hi,

How can I get around runtime error that says I can not explicit cast
List<SubClassto ICollection<Class>?

Generic List inhertis generic ICollection and
Subclass inherits Class,

then should not a problem to cast it explicitly, right?

It is and should be a problem to cast it. If you could cast it, then you
could then treat your List<SubClasscollection as if it was the
more-general ICollection<Class>. And if you can do that, then you can add
an instance of Class to your object that is really a List<SubClass>. And
if you can do that, then all of the sudden you have no way to guarantee
that everything in your List<SubClassis really an instance of SubClass.

Google (the web or this newsgroup) for the keywords "covariant",
"covariance", and "generic". This is a fairly common question and there
is a lot more in-depth discussion in this newsgroup and elsewhere.

As an interesting side-note: C# 4.0 is supposed to include support for
covariant generics. It still won't apply in this case -- they aren't
allowing covariance anywhere that doing so could break something -- but it
will address this very common question in other scenarios where it
actually _is_ safe to cast something in this way.

Pete
Thanks, Pete!

Seems we need a read only collection and allow safe cast.
Nov 13 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.