Generics with inherited Interfaces.

G

Guest

I have a question about using Generics with Interfaces and some of there
inheritance issues / problems. If this is not possible what I describe below
I will have to go a different route and would like some suggestions.

I am unable to use abstract classes as my code must not effect any current
inheritance chains so I am using interfaces. So I have something like this:

interface IBase
{
Collection<IBaseItem> Items{get;set;}
}

interface IBaseItem
{
string Title { get; set; }
}

Now that is the base and then we will have components that will extend this:

interface IAnotherBase : IBase
{
string Information {get; set; }
}

interface IAnotherBaseItem : IBaseItem
{
string OtherStuff { get; set; }
}

The question comes down to implementation. When implementing the second set
of classes the Collection<> should be of IAnotherBaseItem elements. So I
need to be able to cast this stuff out. As well any class that implements
the IAnotherBase class should enforce that the items being added to the
collection are of the correct type of IAnotherBaseItem class.

In trying to do something like this it won't let me:

class Stuff : IAnotherBase
{
private Collection<IAnotherBaseItem> _items;

// this is the interface from IBase
public Collection<IBaseItem> Items
{
// This fails
get { return this._items; }
set { this._items = value; }
}
}

What I need to be able to do is something I guess like interface shadowing
however I need to figure out how to cast this information out. Do I need to
use a secondary internal Collection<> of the IBaseItem as well as the
IAnotherBaseItem or something else?

Anything that anybody can help me with would be great.

Thanks.
 
S

Steven Nagy

I haven't run this code to test if its right, but since you no doubt
have an example there, have you tried casting? Eg.
get { return this._items; }
set { this._items = value; }
becomes

get { return (Collection<IBaseItem>)this._items; }
set { this._items = (Collection<IAnotherBaseItem>)value; }

I'm not sure if you can even cast with generics this way. But I just
thought I'd suggest what seemed most evident to me. Let me know result.

Cheers,
Steven Nagy
 
J

Jon Skeet [C# MVP]

Steven Nagy said:
I haven't run this code to test if its right, but since you no doubt
have an example there, have you tried casting? Eg.


I'm not sure if you can even cast with generics this way. But I just
thought I'd suggest what seemed most evident to me. Let me know result.

No, you can't. For instance, List<string> can't be cast to
List<object>. This is because you would then be able to add an object
to the List<object> and that would violate it being a List<string>.

To put it in a nutshell which may or may not help, C# does not expose
generic covariance or contravariance. If you search with the
appropriate terms, you'll find much longer explanations :)
 
S

Steven Nagy

Yeah I guess it makes sense to me. I mean, whats the point of trying to
make generics, MORE generic by removing the whole type-safety? For me,
I find it great to get a design-time, strongly typed object out of a
"generic" situation, and to do what I suggested before is as bad as
just having a non-generic collection of "object"s and casting. It
totally removes the point of having a generic collection. I totally
take your point. And I'll check those terms... wikipedia?

Also, when's your next planned BLOG post?
 
J

Jon Skeet [C# MVP]

Steven Nagy said:
Yeah I guess it makes sense to me. I mean, whats the point of trying to
make generics, MORE generic by removing the whole type-safety? For me,
I find it great to get a design-time, strongly typed object out of a
"generic" situation, and to do what I suggested before is as bad as
just having a non-generic collection of "object"s and casting. It
totally removes the point of having a generic collection. I totally
take your point. And I'll check those terms... wikipedia?

Have a look at:

http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx

http://www.dotnetgeeks.com/blogs/dotnetgeek/archive/2006/07/31/CoVarian
ce_2C00_-Contravariance-and-Generics.aspx

http://www.dotnetgeeks.com/blogs/dotnetgeek/archive/2006/08/09/Covarian
ce_2C00_-Contravariance-and-Generics-Part-Deux.aspx
Also, when's your next planned BLOG post?

I've got two posts "in the works" but I wouldn't like to say when
they'll be finished...
 
S

Steven Nagy

Have a look at:


The first link was a good read. If I understood it correctly,
covariance is supported naturally by the CLR, but the specific language
compilers have disabled it?
Why not leave it in, and then let the programmer make the decision
about when its good or bad to use it?

The other 2 links didn't work for me, but I understand it better now.
I know you're a Java programmer as well, do you use covariant generics
much there?
(Sorry I don't know Java at all, just more interested in the demand).

SN
 
J

Jon Skeet [C# MVP]

The first link was a good read. If I understood it correctly,
covariance is supported naturally by the CLR, but the specific language
compilers have disabled it?

Yup. At least, the C# spec has effectively disabled it - other
languages could still include it, although I don't *think* C++ or
VB.NET do.
Why not leave it in, and then let the programmer make the decision
about when its good or bad to use it?

<snip>

Well, there's a bit of a difficulty in terms of the standard library -
that would really have to use it to make it useful elsewhere, and that
means the documentation would need to include references to it, which
means it's really "all or nothing" for the mainstream languages. It
*is* a pain to understand. I think I'd prefer it to be present as well,
on balance, but I can understand the decision on grounds of keeping
things simple.

As far as I know (I haven't looked in detail) .NET covariance only
applies to interfaces, too, which is a fairly major restriction. I do
wonder why it's in there at all, given all of these things...
I know you're a Java programmer as well, do you use covariant generics
much there?

I use it a fair amount, yes.
(Sorry I don't know Java at all, just more interested in the demand).

I think it's more obviously needed when it's missing :)

(It's one of the harder-to-understand bits of generics though - I
usually suggest people ignore it until they need it.)
 
S

Steven Nagy

As far as I know (I haven't looked in detail) .NET covariance only
applies to interfaces, too, which is a fairly major restriction. I do
wonder why it's in there at all, given all of these things...

That article you pointed me to implied it was only Interfaces that it
supports.

Thanks for the great info.

A common saying here in Australia is that when you learn something new,
you get to go home!

SN
 

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