Tuesday, 15 June 2010

c# - How to construct an array of strings by passing in a list? -


I tried to search for "C # new string array dynamic" but could not find anything related to it.

  int [] id = someMethodCall (); & Lt; String & gt; Value = new list & lt; String & gt; (); Foreach (i int in id) {values.Add (i.ToString ()); } Some classes sc = new classes (); Sc.Value = new string [] {"values.string1", "values.string2", ...};  

What am I trying to do is to cross the stars by sc.Value, so I do not have to write them (because I do not know what they will do before)

sc.Value is a string [] which I am using from the API (not written by me) is defined by the class.

What is the best way to do this dynamically? In other words, how to know dynamic values ​​for a string [] creation?

If I do not remember anything, you can simply ToArray method

  can use sc.Value = values.ToArray ();  

BTW, you do not have to create a list in the first place:

  sc.Value = someMethodCall (). Select (x = & gt; x.ToString ()). ToArray ();  

No comments:

Post a Comment