469,898 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Generics with inherited Interfaces.

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<IBaseItemItems{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<IBaseItemItems
{
// 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.
Sep 9 '06 #1
7 1890
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

Sep 10 '06 #2
Steven Nagy <le*********@hotmail.comwrote:
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.
No, you can't. For instance, List<stringcan't be cast to
List<object>. This is because you would then be able to add an object
to the List<objectand 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 :)

--
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
Sep 10 '06 #3
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?

Sep 10 '06 #4
Steven Nagy <le*********@hotmail.comwrote:
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/archiv...16/375079.aspx

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

http://www.dotnetgeeks.com/blogs/dot...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...

--
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
Sep 11 '06 #5
Have a look at:
>
http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx

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

http://www.dotnetgeeks.com/blogs/dot...08/09/Covarian
ce_2C00_-Contravariance-and-Generics-Part-Deux.aspx

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

Sep 11 '06 #6
Steven Nagy <le*********@hotmail.comwrote:
Have a look at:

http://blogs.msdn.com/rmbyers/archiv...16/375079.aspx
<snip>
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.)

--
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
Sep 11 '06 #7
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

Sep 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by andrew queisser | last post: by
4 posts views Thread by Chuck Cobb | last post: by
7 posts views Thread by Ajeet | last post: by
32 posts views Thread by Martin | last post: by
6 posts views Thread by =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post: by
3 posts views Thread by =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.