473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inheriting from generic List

I think I must be missing something about generics, perhaps just about the
syntax. I'd like to derive a class MyList from System.Collections.Generic so
that it can only contain instance of MyItem.

No problem with

class MyList<T> : List<T>
{
}

but that requires me to create it with MyList <MyItem> myList = new
MyList<MyItem>() and I'd like to hide that bit in the list class itself so I
could call MyList myList = new MyList();

What should the constructor look like? I think it should be something like
the illegal

class MyList : List<T>
{
public MyList() : base (<T>)
{
}
}

but it obviously isn't! Is this possible or is my understanding of generics
completely wrong?

(Anyone know a good primer on this?)

Andrew
Nov 17 '05 #1
10 2325

"Andrew McLellan" <an****@cix.co.uk> schrieb im Newsbeitrag
news:e5*************@TK2MSFTNGP10.phx.gbl...
I think I must be missing something about generics, perhaps just about the
syntax. I'd like to derive a class MyList from System.Collections.Generic
so that it can only contain instance of MyItem.

No problem with

class MyList<T> : List<T>
{
}

but that requires me to create it with MyList <MyItem> myList = new
MyList<MyItem>() and I'd like to hide that bit in the list class itself so
I could call MyList myList = new MyList();

What should the constructor look like? I think it should be something like
the illegal

class MyList : List<T>
{
public MyList() : base (<T>)
{
}
}

but it obviously isn't! Is this possible or is my understanding of
generics completely wrong?

(Anyone know a good primer on this?)

Andrew

You can try:
class MyList : List<MyItem>
{
public MyList()
{
}
}

Since MyList shall always contain MyItem-objects it's no generic class
itself.
Nov 17 '05 #2
Thank you, exactly what I was after.

Andrew
Nov 17 '05 #3
Andrew McLellan <an****@cix.co.uk> wrote:
Thank you, exactly what I was after.


But at that stage, why not just use List<MyItem> in your code? There's
no need to create a new class here, as far as I can see - unless you're
going to add extra functionality, of course.

--
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
Nov 17 '05 #4
On Thu, 22 Sep 2005 18:26:15 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Andrew McLellan <an****@cix.co.uk> wrote:
Thank you, exactly what I was after.


But at that stage, why not just use List<MyItem> in your code? There's
no need to create a new class here, as far as I can see - unless you're
going to add extra functionality, of course.


Extending or being able to add extensions in the future is of course a
good reason.

The other reason to do it is to specify that the object has a specific
purpose. This is a classic case of Strong vs Weak typing. The
advantage with the subclass is that you attach stronger typing
information to the object. The disadvantage is that you loose some
flexibility.

--
Marcus Andrén

Nov 17 '05 #5
"Marcus Andrén" wrote:

Extending or being able to add extensions in the future is of course a
good reason.
then should you derive from every non-sealed class in the framework before
you use it, in case in the future you might want to extend it? it is a
reason, good or not is debatable in my opinion.

The other reason to do it is to specify that the object has a specific
purpose. This is a classic case of Strong vs Weak typing. The
advantage with the subclass is that you attach stronger typing
information to the object. The disadvantage is that you loose some
flexibility.
List<T> is already strongly typed. you can't put anything that's not T into
the List once T is defined.

--
Marcus Andrén

Nov 17 '05 #6
On Thu, 22 Sep 2005 15:15:04 -0700, "Daniel Jin"
<Da*******@discussions.microsoft.com> wrote:
then should you derive from every non-sealed class in the framework before
you use it, in case in the future you might want to extend it? it is a
reason, good or not is debatable in my opinion.

I agree with you here. Extending just to be prepared for the future is
not really a good reason.

List<T> is already strongly typed. you can't put anything that's not T into
the List once T is defined.


class Log : List<string> {}

public void AttachLog(Log log) {}

List<string> log = new List<string>();
AttachLog(log);

This will not compile since AttachLog expects an object of type Log
and not any random List<string>. This is because Log is more strongly
defined than List<string> even though there is no functional
difference between the two classes.

In most cases I myself wouldn't bother doing it since the improvements
are minimal, although the same can be said about the disadvantages.

I actually have only used it once with generics. I was using a
Dictionary<string,string> to store options and it was being used a lot
so I created an Options class simply because it looked nicer with the
methods taking an Options parameter instead of a
Dictionary<string,string> parameter.

--
Marcus Andrén
Nov 17 '05 #7
My 0010 cents.

Inheritance is seldom The Good Thing. Especially in the example below. The
Log class seem to be very specific and would be better of being a simple
class derived from Object wrapping a generic list.

What I mostly dislike with inheriting from List<T> is that you get a lot of
methods that doesn't set well in context. Like Reverse() and Sort() and
stuff like that.

Happy Coding
- Michael S
"Marcus Andrén" <a@b.c> wrote in message
news:m7********************************@4ax.com...
On Thu, 22 Sep 2005 15:15:04 -0700, "Daniel Jin"
<Da*******@discussions.microsoft.com> wrote:
then should you derive from every non-sealed class in the framework before
you use it, in case in the future you might want to extend it? it is a
reason, good or not is debatable in my opinion.


I agree with you here. Extending just to be prepared for the future is
not really a good reason.

List<T> is already strongly typed. you can't put anything that's not T
into
the List once T is defined.


class Log : List<string> {}

public void AttachLog(Log log) {}

List<string> log = new List<string>();
AttachLog(log);

This will not compile since AttachLog expects an object of type Log
and not any random List<string>. This is because Log is more strongly
defined than List<string> even though there is no functional
difference between the two classes.

In most cases I myself wouldn't bother doing it since the improvements
are minimal, although the same can be said about the disadvantages.

I actually have only used it once with generics. I was using a
Dictionary<string,string> to store options and it was being used a lot
so I created an Options class simply because it looked nicer with the
methods taking an Options parameter instead of a
Dictionary<string,string> parameter.

--
Marcus Andrén

Nov 17 '05 #8
> But at that stage, why not just use List<MyItem> in your code? There's
no need to create a new class here, as far as I can see - unless you're
going to add extra functionality, of course.


I am indeed going to add extra functionality. This isn't necessarily how I
will end up doing it, but I am still trying to understand the C# way of
doing things, which means writing the code the way I think it shouldn't be
written and then seeing why I don't want to do it that way after all!

Andrew

Nov 17 '05 #9
Is the functionality limited to MyItem though? If not, it would be
worth deriving from List<T> but keeping the implementation generic, and
allowing clients to then do SuperDuperList<MyItem> or
SuperDuperList<MyOtherItem>.

Jon

Nov 17 '05 #10
> Is the functionality limited to MyItem though?

Some of it is, some of it isn't, so I'll keep the genericity for one level,
then put a class on top with the specific functionality.

Thanks all for the help.

Andrew
Nov 17 '05 #11

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

Similar topics

15
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element...
29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
0
by: Umut Tezduyar | last post by:
I have a control derived from ComposeteDataBoundControl. Control doesn't retreive it's state information. I am going to frick out! What should I do more? I added two codes. One is for Default.aspx...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
1
by: DotNetNewbie | last post by:
Hi, In the source code for System.Web.MVC the generic class is defined as: public class ViewPage<TViewData: ViewPage I want to create my own custom ViewPage class, and inherit from the...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
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.