Convert int array to string array in C#
This post will discuss how to convert int array to string array in C#.
1. Using Enumerable.Select Method
LINQ’s Enumerable.Select method is commonly used to project each element of a sequence into a new form. The following code example demonstrates the usage of the Select() method for transforming an int array to a string array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Linq; public class Example { public static void Main() { int[] numbers = { 1, 2, 3, 4, 5 }; string[] result = numbers.Select(i => i.ToString()).ToArray(); Console.WriteLine(String.Join(", ", result)); } } |
Output:
1, 2, 3, 4, 5
2. Using Array.ConvertAll Method
The standard solution to convert an array of one type to an array of another type is using the Array.ConvertAll() method. Consider the following example, which converts each element of the specified array from integer type to string type using the specified converter.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int[] numbers = { 1, 2, 3, 4, 5 }; string[] result = Array.ConvertAll(numbers, x => x.ToString()); Console.WriteLine(String.Join(", ", result)); } } |
Output:
1, 2, 3, 4, 5
That’s all about converting int array to string array in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)