Michael,
I am curious, why don't you want the foreach loop? You realize that the
ConvertAll method really just calls foreach on the array itself and then
runs the converter on each element.
While I think the foreach line is probably more concise, you can do the
same with the ConvertAll method, using an anonymous delegate like so:
string[] a = Array.ConvertAll<object, string>(v, delegate(object o) { return
o.ToString(); });
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
(E-Mail Removed)
"Michael Bray" <(E-Mail Removed)> wrote in message
news:Xns991F6BFD037C0mbrayctiusacom@207.46.248.16...
> I'm trying to figure out what is the easiest way in C# 2.0 to convert an
> object array (object[], int[], anything[]) to a string array (string[] or
> List<string>) in one line of code. At first I thought I could do
> something
> like the following:
>
> object[] v; // initialized with some values
>
> return Array.ConvertAll<object,string>
> (v,
> new Converter<object,string>(
> new delegate(object o) {
> return o.ToString();
> }
> )
> );
>
> but it appears that Converter<T,T> requires an actual pointer to a
> function
> that does the conversion, but maybe I'm just not able to figure out the
> syntax correctly.
>
> What I don't want is something like this:
>
> List<string> ss = new List<string>();
> foreach(object o in v) ss.Add(o.ToString());
>
> Any good ideas out there? is it possible? part two of the challenge -
> what if I don't know the exact type of object array? That is, I want a
> generic method to convert an anything[] array to a List<string>.
>
> Have at it!
>
> -mdb