473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1976
uriye <ur***@discussions.microsoft.com> 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<NetworkStream> 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.com>
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<NetworkStream> 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<Stream> 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.net> wrote:
Suppose you have List<FileStream> and try to use it as a List<Stream> -
you would be able to add a List<NetworkStream> 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<Stream> 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.com>
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<Stream> 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
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...
3
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...
6
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...
5
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...
12
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...
2
by: Daniel Billingsley | last post by:
Let's say I have: public class RootClass<T> { public virtual T GetOne() { return default(T); } }
5
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...
8
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...
8
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.