25

I've got :

string[] strArray = new string[3] { "1", "2", "12" };

And I want something like this

int[] intArray = Convert.toIntArray(strArray);

I work int C# .net v2.0, I don't want to write a lot of code.

How can I do that?

Thank you.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Christophe Debove
  • 6,088
  • 20
  • 73
  • 124
  • .Net 3.0+ version for people from google: `int[] intArr = strArr.Select(int.Parse).ToArray()` – Callum Rogers Dec 08 '10 at 13:31
  • (embarassing comment removed) –  Dec 08 '10 at 13:33
  • @Callum: See http://stackoverflow.com/questions/1297231/convert-string-to-int-in-one-string-of-code-using-linq ;) P.S. Your code isn't the best you can get for the task – abatishchev Dec 08 '10 at 13:34
  • Possible duplicate of [Convert string\[\] to int\[\] in one line of code using LINQ](https://stackoverflow.com/questions/1297231/convert-string-to-int-in-one-line-of-code-using-linq) – Andrew Neate Jan 13 '18 at 12:49

6 Answers6

47

You can use the Array.ConvertAll method for this purpose, which "converts an array of one type to an array of another type."

int[] intArray = Array.ConvertAll(strArray,
                                  delegate(string s) { return int.Parse(s); });

(EDIT: Type-inference works fine with this technique. Alternatively, you could also use an implicit method-group conversion as in Marc Gravell's answer, but you would have to specify the generic type-arguments explicitly.)

Using a for-loop:

int[] intArray = new int[strArray.Length];

for (int i = 0; i < strArray.Length; i++)
   intArray[i] = int.Parse(strArray[i]);

For completeness, the idiomatic way of doing this in C# 4.0 would be something like:

var intArray = strArray.Select(int.Parse).ToArray();

or:

//EDIT: Probably faster since a fixed-size buffer is used
var intArray = Array.ConvertAll(strArray, int.Parse);
Ani
  • 111,048
  • 26
  • 262
  • 307
19
int[] intArray = Array.ConvertAll(strArray, int.Parse);

or in C# 2.0 (where the generic type inference is weaker):

int[] intArray = Array.ConvertAll<string,int>(strArray, int.Parse);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2
using System.Collections.Generic;

int Convert(string s)
{
    return Int32.Parse(s);
}

int[] result = Array.ConvertAll(input, new Converter<string, int>(Convert));

or

int[] result = Array.ConvertAll(input, delegate(string s) { return Int32.Parse(s); })
abatishchev
  • 98,240
  • 88
  • 296
  • 433
1

Array.ConvertAll Generic Method

Converts an array of one type to an array of another type.

CD..
  • 72,281
  • 25
  • 154
  • 163
1

When you are sure, all items are definitely parsable, this will do the trick:

string[] strArray = new string[3] { "1", "2", "12" };
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
    intArray[i] = int.Parse(strArray[i]);
}
fjdumont
  • 1,517
  • 1
  • 9
  • 22
0

Something like this, but you want to keep a bit of error checking ( my if is really lame, just as example ):

static private int[] toIntArray(string[] strArray)
{
    int[] intArray = new int[strArray.Length];
    for (int index = 0; index < strArray.Length; index++)
    {
            intArray[index] = Int32.Parse(strArray[index]);
    }
    return intArray;
}

And use it like this:

string[] strArray = new string[3] { "1", "2", "12" };
int[] interi = toIntArray(strArray);
SimoneF
  • 432
  • 1
  • 3
  • 15
  • 3
    Int32.Parse can't return null - it returns an int. – Jon Skeet Dec 08 '10 at 13:32
  • `Int32.Parse()` returns `Int32` that is struct (value type), thus `!= null` always passes. This line of code generates a warning concerning what am I talking about. – abatishchev Dec 08 '10 at 13:33