Casting generics and inherited classes in .NET 2.0

G

Guest

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());
}
}
 
J

Joanna Carter [TeamB]

"Dave Booker" <[email protected]> a écrit dans le message de (e-mail address removed)...

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

Guest

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

Jon Skeet [C# MVP]

Dave Booker said:
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.
 
J

Joanna Carter [TeamB]

"Dave Booker" <[email protected]> a écrit dans le message de (e-mail address removed)...

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

Joanna Carter [TeamB]

"Jon Skeet [C# MVP]" <[email protected]> a écrit dans le message de (e-mail address removed)...

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

Joanna Carter [TeamB]

"Joanna Carter [TeamB]" <[email protected]> a écrit dans le message de
%[email protected]...

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

Jon Skeet [C# MVP]

Joanna Carter said:
"Jon Skeet [C# MVP]" <[email protected]> a écrit dans le message de
(e-mail address removed)...

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

TerryFei

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top