471,325 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

extending generics?

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
Feb 7 '06 #1
6 3897
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tbh" <fe****@newsgroups.nospam> wrote in message
news:uB**************@TK2MSFTNGP09.phx.gbl...
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

Feb 7 '06 #2
tbh <fe****@newsgroups.nospam> wrote:
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.

--
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
Feb 7 '06 #3
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>);

Feb 7 '06 #4
kevin cline <ke*********@gmail.com> wrote:
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 :)

--
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
Feb 8 '06 #5
It does indeed! Somewhere I got the notion that C# did no type
inferencing at all.

Feb 8 '06 #6
tbh
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);
}

Feb 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by NadaHombre | last post: by
27 posts views Thread by Bernardo Heynemann | last post: by
23 posts views Thread by Luc Vaillant | last post: by
20 posts views Thread by Keith Elder | last post: by
9 posts views Thread by sloan | last post: by
1 post views Thread by Vladimir Shiryaev | last post: by
7 posts views Thread by SpotNet | last post: by
13 posts views Thread by rkausch | last post: by
reply views Thread by rosydwin | last post: by

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.