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 6 3978
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
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
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 <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
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);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 can´t use
generics...
I´m trying to make a generic collection myCollection<vartype> and...
|
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...
|
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...
|
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
...
|
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");
...
|
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...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
| |