Challenge: array conversion in 1 line

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
 
J

Jon Skeet [C# MVP]

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.

Is there any reason you don't want to write a helper method which uses
the simple foreach loop that you showed, and call that helper in one
line?

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Marc Gravell

return Array.ConvertAll<object,string>(v, delegate(object obj) {return
obj.ToString();});

(or Convert.ToString(obj); inside the inline method)

Marc
 
P

PS

Michael Bray said:
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.

"easiest" and "one line of code" can be conflicting and subjective
requirements. A helper method will give you a one line solution where you
need to use it, and the code in the helper method can be written easily.

PS

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
 
M

Michael Bray

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(); });

Yeah as you all surmised, there's no good reason for what I wanted...
more than anything else I just was wondering if it was possible, and sprung
out of my desire to use the syntax you show above, not necessarily for this
specific example, but just in general... (that would be classified as an
anonymous method, yes?) As I mentioned, I just wasn't getting the syntax
right. Thanks!

So the next question would be - is there a performance penalty if I do it
this way (with the inline delegate) instead of a foreach loop and not using
the delegate?

-mdb
 
M

Marc Gravell

So the next question would be - is there a performance penalty if I do it
this way (with the inline delegate) instead of a foreach loop and not using
the delegate?

Probably... delegate invoke is a little slow (but better than it used
to be)... but why not put together a volume test and find out ;-p

Generally speaking, though; such considerations are best left for when
you can profile the solution as a whole, otherwise you risk premature
optimisation - and investing lots of time improving stuff that simply
isn't a bottleneck.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ToString and FromString 10
array conversion 2
String Array to List 4
passing integer array as object array 5
Enum To String 14
In array? 2
byte[] to string conversion ... 5
Trying to split an array on a tab 5

Top