Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to convert integer array to string array
Converting an integer array to a string array is a common task in C# programming. The most efficient approach is using the Array.ConvertAll() method, which applies a conversion function to each element in the source array and returns a new array of the target type.
Syntax
Following is the syntax for Array.ConvertAll() method −
string[] result = Array.ConvertAll(sourceArray, converter);
Parameters
-
sourceArray − The source integer array to convert
-
converter − A lambda expression or method that converts each element
Return Value
Returns a new string array containing the converted elements from the source array.
Using Array.ConvertAll() with Lambda Expression
The most common approach uses a lambda expression to convert each integer to its string representation −
using System;
public class Demo {
public static void Main() {
int[] intArray = new int[5];
// Initialize integer array with 5 elements
intArray[0] = 15;
intArray[1] = 30;
intArray[2] = 44;
intArray[3] = 50;
intArray[4] = 66;
// Convert integer array to string array
string[] strArray = Array.ConvertAll(intArray, ele => ele.ToString());
Console.WriteLine("Original integer array: [" + string.Join(", ", intArray) + "]");
Console.WriteLine("Converted string array: [" + string.Join(", ", strArray) + "]");
}
}
The output of the above code is −
Original integer array: [15, 30, 44, 50, 66] Converted string array: [15, 30, 44, 50, 66]
Using Array.ConvertAll() with Method Group
You can also use method group syntax for cleaner code −
using System;
public class Demo {
public static void Main() {
int[] numbers = {10, 20, 30, 40, 50};
// Convert using method group syntax
string[] stringNumbers = Array.ConvertAll(numbers, Convert.ToString);
Console.WriteLine("Using Convert.ToString method:");
foreach (string str in stringNumbers) {
Console.WriteLine("'" + str + "' (Type: " + str.GetType().Name + ")");
}
}
}
The output of the above code is −
Using Convert.ToString method: '10' (Type: String) '20' (Type: String) '30' (Type: String) '40' (Type: String) '50' (Type: String)
Using LINQ Select Method
An alternative approach uses LINQ's Select() method combined with ToArray() −
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] intArray = {100, 200, 300, 400, 500};
// Convert using LINQ Select
string[] strArray = intArray.Select(x => x.ToString()).ToArray();
Console.WriteLine("Using LINQ Select:");
Console.WriteLine("[" + string.Join(" | ", strArray) + "]");
// Verify the type conversion
Console.WriteLine("First element type: " + strArray[0].GetType().Name);
}
}
The output of the above code is −
Using LINQ Select: [100 | 200 | 300 | 400 | 500] First element type: String
Comparison
| Method | Performance | Readability | Additional Dependencies |
|---|---|---|---|
| Array.ConvertAll() | Fastest | Good | None |
| LINQ Select().ToArray() | Slightly slower | Very good | System.Linq |
| Manual loop | Fast | More verbose | None |
Conclusion
The Array.ConvertAll() method is the most efficient way to convert an integer array to a string array in C#. It provides excellent performance and requires no additional dependencies. For functional programming enthusiasts, LINQ's Select().ToArray() offers a more expressive alternative with minimal performance overhead.
