C # one liner for .ToString () for the whole array

I feel stupid to ask, but there should be one liner, which is the equivalent or almost the equivalent of the code below in C # ... so can you tell me what it is?

public static string[] ToStringArray(int[] i) { if (i==null) return null; string[] result = new string[i.Length]; for (int n= 0; n< result.Length; n++) result[n] = i[n].ToString(); return result; } 
+4
source share
4 answers

What about the extension method?

 public static string[] ToStringArray<T>(this IEnumerable<T> items) { return items.Select(i => i.ToString()).ToArray(); } 
+10
source

Using LINQ:

 int[] ints = { 1, 2, 3 }; string[] strings = ints.Select(i => i.ToString()).ToArray(); 
+9
source

Using LINQ:

 (from x in i select x.ToString()).ToArray() 
+2
source
 int[] x = new int[] {1,2,3}; string[] y = Array.ConvertAll(x, intArg => intArg.ToString());
int[] x = new int[] {1,2,3}; string[] y = Array.ConvertAll(x, intArg => intArg.ToString()); 
+1
source

All Articles