HOWTO · Csharp
How to Convert String Array to Int Array in C#
In this guide, we will learn how to convert a string array to an int array in C#.
On this page
This guide shows how to convert a string array to an int array in C#. Because every element arrives as text, each value has to be parsed into an integer. Three patterns cover the task: Array.ConvertAll, a LINQ Select projection with int.Parse, and the same projection backed by int.TryParse for input that might not parse cleanly.
Convert a string array with Array.ConvertAll
Array.ConvertAll applies a converter function to every element of a source array and returns a new array of the target type. Passing the int.Parse method group as the converter parses each string in a single statement.
Run this example:
using System;
class Program {
static void Main() {
string[] temp_str = new string[] { "1000", "2000", "3000" };
int[] temp_int = Array.ConvertAll(temp_str, int.Parse);
foreach (int num in temp_int) {
Console.WriteLine(num);
}
}
}
1000
2000
3000
The result is a new int[]; the original string[] stays unchanged. Array.ConvertAll is a good default when every element should be converted the same way and no further projection is needed. Because int.Parse performs the conversion, every element must be a well-formed integer within the Int32 range, and the parsing rules described in the next sections apply here as well.
Convert a string array with LINQ Select and int.Parse
LINQ’s Select method projects each element of the source sequence through a function. Select(int.Parse) produces an IEnumerable<int> rather than an array, so ToArray is required to materialize the results into an int[]. This form fits naturally when the conversion is part of a longer LINQ pipeline, such as filtering values before parsing them.
Run this example:
using System;
using System.Linq;
class Program {
static void Main() {
string[] temp_str = new string[] { "1000", "2000", "3000" };
int[] temp_int = temp_str.Select(int.Parse).ToArray();
foreach (int num in temp_int) {
Console.WriteLine(num);
}
}
}
1000
2000
3000
Note that Select is lazy: nothing is parsed until the sequence is enumerated, and ToArray performs that enumeration exactly once. int.Parse is strict — it throws a FormatException when a value is not numeric and an OverflowException when it falls outside the Int32 range. Both patterns above share that behavior, which is what the third approach addresses.
Handle invalid values with int.TryParse
int.TryParse returns a bool instead of throwing, so it tolerates values that cannot be parsed, and it writes the parsed number to an out parameter that the lambda exposes as result. Inside Select, the lambda returns the parsed value on success and a chosen fallback otherwise. The program below substitutes 0 for any value that fails to parse:
using System;
using System.Linq;
class Program {
static void Main() {
string[] temp_str = new string[] { "1000", "abc", "3000" };
int[] temp_int = temp_str.Select(s => int.TryParse(s, out int result) ? result : 0).ToArray();
foreach (int num in temp_int) {
Console.WriteLine(num);
}
}
}
1000
0
3000
Pick the fallback deliberately: a substituted 0 is indistinguishable from a genuine 0 in the result. If that ambiguity is unacceptable, record the failures separately — filter the unparsed values out, use a sentinel such as -1, or collect the offending strings for logging.
For a quick choice between the patterns, reach for Array.ConvertAll when a direct one-call conversion is all that is needed, Select with int.Parse when the conversion sits inside a LINQ pipeline, and int.TryParse when the input is untrusted and a documented fallback is acceptable.