PC Review


Reply
Thread Tools Rate Thread

Challenge: array conversion in 1 line

 
 
Michael Bray
Guest
Posts: n/a
 
      27th Apr 2007
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
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th Apr 2007
On Apr 27, 3:35 pm, Michael Bray
<mbrayATctiusaDOT...@you.figure.it.out.com> wrote:
> 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

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      27th Apr 2007
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



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      27th Apr 2007
return Array.ConvertAll<object,string>(v, delegate(object obj) {return
obj.ToString();});

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

Marc


 
Reply With Quote
 
PS
Guest
Posts: n/a
 
      27th Apr 2007
"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.


"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



 
Reply With Quote
 
Michael Bray
Guest
Posts: n/a
 
      30th Apr 2007
"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote
in news:(E-Mail Removed):

> 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
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      30th Apr 2007
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Challenge - Excel Line Feed Character CHR(10) - How to Delete and keep the text formatting without going ro single line in a cell ? Microsoft Excel Programming 6 7th Oct 2009 12:28 PM
Challenge - Excel Line Feed Character CHR(10) - How to Delete and keep the text formatting without going ro single line in a cell ? Microsoft Excel Worksheet Functions 7 7th Oct 2009 11:10 AM
challenge! ISNA() and complex array formula Lorin Microsoft Excel Misc 3 3rd Jan 2006 04:04 AM
Sorting Challenge for Array Wizards Captain_Nemo@example.com Microsoft Excel Worksheet Functions 5 15th May 2004 01:06 PM
Array formula challenge Laurence Lombard Microsoft Excel Misc 1 12th Nov 2003 12:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:31 PM.