How to create my own generic list?

L

LJB

I'm still new at C# or any OO program and my coworkers are all at a seminar
this week.

How do I create my own "generic list" that implements all the features of
the .net generic list<string> plus a method to fill itself from strings in
an external file. Is there a simple template somewhere I could start with?
Do I just inherit from generic list and add my method?

thanks,
LJB
 
J

Jeff Johnson

I'm still new at C# or any OO program and my coworkers are all at a
seminar this week.

How do I create my own "generic list" that implements all the features of
the .net generic list<string> plus a method to fill itself from strings in
an external file. Is there a simple template somewhere I could start with?
Do I just inherit from generic list and add my method?

Without knowing any more about your situation, inheriting from List<string>
sounds like a good start.
 
P

Patrice

Do I just inherit from generic list and add my method?

Sounds good enough to try this before posting ;-)
 
P

Peter Duniho

LJB said:
I'm still new at C# or any OO program and my coworkers are all at a seminar
this week.

How do I create my own "generic list" that implements all the features of
the .net generic list<string> plus a method to fill itself from strings in
an external file. Is there a simple template somewhere I could start with?
Do I just inherit from generic list and add my method?

IMHO, there should be no need to inherit from the generic type.
Instead, I would write an extension method for List<T> to accomplish
your task. This will have the advantage of allowing your method to
work, but without adding a new type to the program and the potential
complications that result in that (e.g. not having to worry about
whether a method parameter should have the new type or the old, being
able to work with any instance of List<T> regardless of source, etc.)

You may even want to write the extension method for IList<T> instead of
List<T> for maximum flexibility.

Yet another approach that IMHO is probably better than sub-classing
List<T> would be to write an iterator method that returns an
IEnumerable<T> given the file path, and which you can pass to the
constructor of List<T>.

All of these suggestions implement the functionality you're looking for
in a much more reusable way, since they don't tie you to a specific
type, or even a specific collection class.

Pete
 
J

Jeff Johnson

IMHO, there should be no need to inherit from the generic type. Instead, I
would write an extension method for List<T> to accomplish your task.

This requires Visual Studio 2008 and later, right? (I.e., C# 3.0.)
 
P

Peter Duniho

Jeff said:
This requires Visual Studio 2008 and later, right? (I.e., C# 3.0.)

For an extension method, yes. But even a static utility method (which
is all an extension method really is) would accomplish the same basic
goal with the same advantages as the extension method. Only the syntax
of the call would be slightly different (i.e. the class containing the
static utility method, most likely a static class, would have to be
specified in the call, and the instance passed as a parameter rather
than being the target of the call).

For iterator methods, that's in C# 2.0. And it may actually be more
valuable and reusable to the OP to write an iterator method to read the
file, that being a more general-purpose approach than even a method that
fills an IList<T> from a file.

Pete
 

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