extending generics?

  • Thread starter Thread starter tbh
  • Start date Start date
T

tbh

hi,

i love generic collections.

one thing I very frequently want to be able to do is take say a List<T> and
join it into a string with a certain delimiter. (in each case the underlying
T object's ToString() should be called.) for any given T I know how to do
this. i guess I can probably even figure out how to build my own MyList<T>
generic with this property.

what I'd like, though, would be for List<T> to have this property.

is this hard? easy? a bad idea?

cheers,

Tim Hanson
 
Tim,

Well, there is no reason you can't extend List<T> to include your
property. It's a class, like any other, and you can extend it like any
other, the same rules apply (not being able to extend sealed classes, etc,
etc).

Hope this helps.
 
tbh said:
i love generic collections.

one thing I very frequently want to be able to do is take say a List<T> and
join it into a string with a certain delimiter. (in each case the underlying
T object's ToString() should be called.) for any given T I know how to do
this. i guess I can probably even figure out how to build my own MyList<T>
generic with this property.

what I'd like, though, would be for List<T> to have this property.

is this hard? easy? a bad idea?

Well, you can't change List<T> to have a property it doesn't already
have. Deriving a new type MyList<T> is the way to go.
 
My first instinct would be to write a static function in some utility
class, but then I started to write the code and realized I had no idea
how to do it.

I started to say:

class ListUtilities
{
public static string JoinList<T>(List<T> list) { ... }
}

...

List<int> myList = new List<int>;
...
ListUtilities.JoinList(myList);

but I don't think that will work in C# because the template parameter
(T) does not appear explicitly in the argument list for JoinList. Now
I'm wondering if there is anyway to define JoinList so that I can
write:
ListUtilities.JoinList(myList)
instead of having to say somethign like:
ListUtilities.JoinList<int>(myList>);
 
kevin cline said:
My first instinct would be to write a static function in some utility
class, but then I started to write the code and realized I had no idea
how to do it.

I started to say:

class ListUtilities
{
public static string JoinList<T>(List<T> list) { ... }
}

...

List<int> myList = new List<int>;
...
ListUtilities.JoinList(myList);

but I don't think that will work in C# because the template parameter
(T) does not appear explicitly in the argument list for JoinList.

That's okay - it doesn't have to. In that case at least, type inference
will work I believe. Give it a try :)
 
It does indeed! Somewhere I got the notion that C# did no type
inferencing at all.
 
thanks, Jon, Nicholas, and Kevin, for your thoughts!

since I'd rather not change all my List<T> calls to MyList<T>, i tried to
figure out something along the lines that Kevin suggested, but also didn't
succeed at first brush.

however, my colleague at the next desk had already invented a pretty usable
approach that lets one write, in effect,
Join(myListInstance.GetEnumerator(), "], [")
instead of something like
myListInstance.Join("], [")
that's slightly clunkier to write, but not so bad (and *much* better than
building a new function for every Type T I want to get a join of a List<T>.
we suspect we should be able to simplify it by using a different interface,
but haven't found time yet.

i'll attach what he did.

cheers,

Tim Hanson


public static string Join(System.Collections.IEnumerator sammlung) {
return Join(sammlung, ",");
}

public static string Join(System.Collections.IEnumerator sammlung,
string Delimeter) {
StringBuilder sb1 = new StringBuilder();
Join(sammlung, Delimeter, sb1);
return sb1.ToString();
}

public static void Join(System.Collections.IEnumerator sammlung, string
Delimeter, StringBuilder Output) {
if (!sammlung.MoveNext()) return;
Output.Append(sammlung.Current.ToString());
while (sammlung.MoveNext()) {
Output.Append(Delimeter);
Output.Append(sammlung.Current.ToString());
}
return;
}

public static string JoinArray(System.Array arrData) {
return Join(arrData.GetEnumerator());
}

public static string JoinArray(System.Array arrData, string Delimeter) {
return Join(arrData.GetEnumerator(), Delimeter);
}

public static void JoinArray(System.Array arrData, string Delimeter,
StringBuilder Output) {
Join(arrData.GetEnumerator(), Delimeter, Output);
}
 
Back
Top