471,325 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Casting generics and inherited classes in .NET 2.0

Is the following behavior correct? If so, can somebody explain why?

public interface IObservation
{
string ID { get; set;}
}
public class Observation
{
string m_ID;
string ID { get { return m_ID; } set { m_ID = value; } }
}
public class ObservationComparer : IComparer<IObservation>
{
public int Compare(IObservation x, IObservation y)
{
return string.Compare(x.ID, y.ID);
}
}
class Program
{
static void Main(string[] args)
{
List<Observation> test = new List<Observation>(); // Will cause
compile error
IList<Observation> test = new List<Observation>(); // Successful
compile
((List<IObservation>)test).Sort(new ObservationComparer());
}
}

Dec 22 '05 #1
8 5922
"Dave Booker" <db******@newsgroup.nospam> a écrit dans le message de news:
07**********************************@microsoft.com...

| List<Observation> test = new List<Observation>();

| ((List<IObservation>)test).Sort(new ObservationComparer());

If you mean, why can't you cast a List<Observation> to a List<IObservation>,
then you need to realise that just becaue Observation implements
IObservation does not imply that a List<> of one tpye can be cast to a
List<> of a derived or base type. Generic types are typesafe to the type
they are bound to.

A List<Observation> is a totally differnt type from List<IObservation>.
There is no inheritance between the two; they are two totally different
List<> types, even though the bound parameter of one does inherit from the
other.

i.e.

IObservation <- Observation // inheritance

List<IObservation> // one strict type
List<Observation> // another strict type

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 22 '05 #2
I suspected as much, but then I still don't understand why an
IList<Observation> can be cast to List<IObservation>.

"Joanna Carter [TeamB]" wrote:
If you mean, why can't you cast a List<Observation> to a List<IObservation>,
then you need to realise that just becaue Observation implements
IObservation does not imply that a List<> of one tpye can be cast to a
List<> of a derived or base type. Generic types are typesafe to the type
they are bound to.

A List<Observation> is a totally differnt type from List<IObservation>.
There is no inheritance between the two; they are two totally different
List<> types, even though the bound parameter of one does inherit from the
other.

i.e.

IObservation <- Observation // inheritance

List<IObservation> // one strict type
List<Observation> // another strict type

Joanna


Dec 22 '05 #3
Dave Booker <db******@newsgroup.nospam> wrote:
I suspected as much, but then I still don't understand why an
IList<Observation> can be cast to List<IObservation>.


It can't. It can be cast to List<Observation>, but not
List<IObservation>.

Basically the type itself still obeys polymorphism, but two types which
use different generic type parameters are completely separate.

--
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
Dec 22 '05 #4
"Dave Booker" <db******@newsgroup.nospam> a écrit dans le message de news:
CD**********************************@microsoft.com...

|I suspected as much, but then I still don't understand why an
| IList<Observation> can be cast to List<IObservation>.

Because IList and List are related *and* IObservation can be regarded as
intrinsically the same type as any class that implements it.

Tricky, isn't it ? :-))

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 22 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...

| > I suspected as much, but then I still don't understand why an
| > IList<Observation> can be cast to List<IObservation>.
|
| It can't. It can be cast to List<Observation>, but not
| List<IObservation>.

After posting my latest reply, I now recant and agree with you John. I had
compiled the test code but hadn't run it. Sure enough, it generates an
invalid typecast :-)

I take it I am correct in saying that that you can cast the generic type to
its ancestor/derivative types, but you have to maintain the parameter type
to be absolutely identical ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 22 '05 #6
"Joanna Carter [TeamB]" <jo****@not.for.spam> a écrit dans le message de
news: %2****************@TK2MSFTNGP10.phx.gbl...

| Because IList and List are related *and* IObservation can be regarded as
| intrinsically the same type as any class that implements it.
|
| Tricky, isn't it ? :-))

Dave, ignore this rubbish, I should have known better than to compile but
not run the test code :-))

See my reply to John's post.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 22 '05 #7
Joanna Carter [TeamB] <jo****@not.for.spam> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...

| > I suspected as much, but then I still don't understand why an
| > IList<Observation> can be cast to List<IObservation>.
|
| It can't. It can be cast to List<Observation>, but not
| List<IObservation>.

After posting my latest reply, I now recant and agree with you John. I had
compiled the test code but hadn't run it. Sure enough, it generates an
invalid typecast :-)

I take it I am correct in saying that that you can cast the generic type to
its ancestor/derivative types, but you have to maintain the parameter type
to be absolutely identical ?


Yup.

From the draft of the ECMA spec:

<quote>
No special conversions exist between constructed reference types other
than those described in §15. In particular, unlike array types,
constructed reference types do not exhibit =3Fco-variant=3F conversions..
This means that a type List<B> has no conversion (either implicit or
explicit) to List<A> even if B is derived from A. Likewise, no
conversion exists from List<B> to List<object>.

[Note: The rationale for this is simple: if a conversion to List<A> is
permitted, then apparently, one can store values of type A into the
list. However, this would break the invariant that every object in a
list of type List<B> is always a value of type B, or else unexpected
failures can occur when assigning into collection classes. end note]
</quote>

and

<quote>
A constructed class type has a direct base class, just like a simple
class type. If the generic class declaration does not specify a base
class, the base class is object. If a base class is specified in the
generic class declaration, the base class of the constructed type is
obtained by substituting, for each type-parameter in the base class
declaration, the corresponding type-argument of the constructed type.
</quote>

--
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
Dec 22 '05 #8
Hi ,
I just wanted to check how things are going and whether or not your
question has been resolved. If there is any question, please feel free to
join the community and we are here to support you at your convenience.
Thanks again and Happy New Year!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Dec 28 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by c | last post: by
4 posts views Thread by Chuck Cobb | last post: by
5 posts views Thread by anders.forsgren | last post: by
3 posts views Thread by Showjumper | last post: by
7 posts views Thread by Ajeet | last post: by
3 posts views Thread by psyCK0 | last post: by
19 posts views Thread by jan.loucka | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.