Search code examples
c#.netarrayslinqlinq-to-objects

Conversion from Int array to string array


When I am converting array of integers to array of string, I am doing it in a lengthier way using a for loop, like mentioned in sample code below. Is there a shorthand for this?

The existing question and answers in SO are about int[] to string (not string[]). So they weren't helpful.

While I found this Converting an int array to a String array answer but the platform is Java not C#. Same method can't be implemented!

int[] intarray =  { 198, 200, 354, 14, 540 };
Array.Sort(intarray);
string[] stringarray = { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty};

for (int i = 0; i < intarray.Length; i++)
{
    stringarray[i] = intarray[i].ToString();
}

Solution

  • int[] intarray = { 1, 2, 3, 4, 5 };
    string[] result = intarray.Select(x=>x.ToString()).ToArray();