473,651 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics and inheritance

Why isn't List<A> a subclass of List<B> if A is a subclass of B ?

This fact causes a lot of design problems. Making the generic collections
loose their type safty strength in large designs.
Maybe there is a design pattern to overcome this problem ? Or maybe someone
knows other related facts ?

Nov 16 '05 #1
4 1988
uriye <ur***@discussi ons.microsoft.c om> wrote:
Why isn't List<A> a subclass of List<B> if A is a subclass of B ?

This fact causes a lot of design problems. Making the generic collections
loose their type safty strength in large designs.
Maybe there is a design pattern to overcome this problem ? Or maybe someone
knows other related facts ?


Suppose you have List<FileStream > and try to use it as a List<Stream> -
you would be able to add a List<NetworkStr eam> to it, which wouldn't be
good. (This is how arrays work, with runtime exceptions, and I'm not at
all sure it's ideal.)

So in fact, currently we *do* have type safe collections - but with
your suggestion, they wouldn't be type safe any more.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Jon,
Why isn't List<A> a subclass of List<B> if A is a subclass of B ?

This fact causes a lot of design problems. Making the generic collections
loose their type safty strength in large designs.
Maybe there is a design pattern to overcome this problem ? Or maybe someone
knows other related facts ?

Suppose you have List<FileStream > and try to use it as a List<Stream> -
you would be able to add a List<NetworkStr eam> to it, which wouldn't be
good. (This is how arrays work, with runtime exceptions, and I'm not at
all sure it's ideal.)

So in fact, currently we *do* have type safe collections - but with
your suggestion, they wouldn't be type safe any more.


Hmm, I don't get that explanation. Supposing I have a method

void LoadFrom(Stream stm)

That method can be type-safely called with a XyzStream, as long
the XyzStream is a subclass of Stream.

Now extrapolated:

void LoadFrom(List<S tream> list)

I'd naively expect to be able to pass a List<XyzStream> object
to that method, too.

bye
Rob
Nov 16 '05 #3
Robert Jordan <ro*****@gmx.ne t> wrote:
Suppose you have List<FileStream > and try to use it as a List<Stream> -
you would be able to add a List<NetworkStr eam> to it, which wouldn't be
good. (This is how arrays work, with runtime exceptions, and I'm not at
all sure it's ideal.)

So in fact, currently we *do* have type safe collections - but with
your suggestion, they wouldn't be type safe any more.
Hmm, I don't get that explanation. Supposing I have a method

void LoadFrom(Stream stm)

That method can be type-safely called with a XyzStream, as long
the XyzStream is a subclass of Stream.


Indeed - because any change to the value of stm is only visible within
the method. Note that if it were a pass-by-reference parameter, you
couldn't, for precisely the reason given below.
Now extrapolated:

void LoadFrom(List<S tream> list)

I'd naively expect to be able to pass a List<XyzStream> object
to that method, too.


And what would you expect to happen if LoadFrom contained:

list.Add (new MemoryStream()) ;

?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi Jon,
Now extrapolated:

void LoadFrom(List<S tream> list)

I'd naively expect to be able to pass a List<XyzStream> object
to that method, too.

And what would you expect to happen if LoadFrom contained:

list.Add (new MemoryStream()) ;


Ok. Got it.

Thanks
Rob
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2010
by: Eddie | last post by:
Hi, I've recently read the articles on generics in the .net framework (the next version) and just wanted to check a few points.. As I understand, generic class instances while being strongly typed checked at compile time, are only created at runtime?? Is this correct??
3
1480
by: Pierre Y. | last post by:
Hi, I've just read that article : http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp I'm asking something. Why are generics constraints not been implemented using Attributes instead of "where..." syntax ? Regards,
6
1962
by: Thomas Tomiczek | last post by:
Ok, working my way through a complex library conversion to .NET 2.0 and C# 2.0 with generics I am totally stuck on one thing -if anyone sees the issue I would be more than glad. The situation is that we have a tree of "Node" objects. A Node is a visualizable item, like a component. THe menues of all or ouf applications, for example, are node hierarchies that an adapter transposes into a menu model. Now, Nodes have an internal...
5
1158
by: PeterLawrance | last post by:
Hi, I would expect that inheritance through the parameters of a generic class would be fine however I get errors as below in the following code. Public Class Form1 Sub test() Dim xxx As Foo(Of DerivedBase) Dim zzz As Foo(Of Base)
12
2734
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as it defines the language more completely. Great to use.
2
4229
by: Daniel Billingsley | last post by:
Let's say I have: public class RootClass<T> { public virtual T GetOne() { return default(T); } }
5
2916
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have an "AppleBasket" which is a class that contains a collection of apples. So, some code: class Fruit{ }
8
2413
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public MyClass() { } public void AppendItem()
8
3172
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem I'm having is that the partial class signature in my projectDetails.ascx.cs file looks like this: public partial class ProjectDetailsControl<TEntryServiceProvider: UserControl, INamingContainer where TEntryServiceProvider : IEntryServiceProvider...
6
5451
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class type constraint 'System.Windows.Forms.ComboBox' must come before any other constraints If I switch place of ComboBox with TextBoxBase, it would gripe:
0
8357
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.