M
Michael Bray
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
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