473,242 Members | 2,784 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,242 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 3970
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: NadaHombre | last post by:
Hello, If I want to make a class which is a stack of objects of some specific type (not just "Objects"), what is the best way to do it? I wonder if there is some generally accepted "best" way to...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still cant use generics... Im trying to make a generic collection myCollection<vartype> and...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
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...
7
by: Paulustrious | last post by:
How do I extend a template class for a specific template data type? I am trying to achieve something like.... public class SomeItem{} public class SomeProcess < T> { } and now the extension ...
20
by: Keith Elder | last post by:
I ran into a unique situation today whereby I have a core library that uses generics to return users from Active Directory. Example: List<ADUser> users = ADUser.GetByName("First", "Last"); ...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.