Convert String Array to Int Array in C#

Haider Ali Jan 30, 2023 Mar 04, 2022
  1. Use Array.ConvertAll() Method to Convert String Array to Int Array in C#
  2. Use LINQ’s Select() Method to Convert String Array to Int Array in C#
Convert String Array to Int Array in C#

This guide will teach us to convert a string array to an int array in C#.

There are two methods to convert String to int. Both of these methods are quite simple and are very easy to implement.

Use Array.ConvertAll() Method to Convert String Array to Int Array in C#

Whenever we talk about converting a string to a different data type by understanding its content, it involves parsing. For instance, string 321 can be converted to 321.

The first method you can use is the Array.ConvertAll() method. Let’s see the implementation of this method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace Array_of_String_to_integer
{
    class Program
    {
        static void Main(string[] args)
        {
            //method 1 using Array.ConvertAll
         	string[] temp_str = new string[] { "1000", "2000", "3000" };
            int[]  temp_int = Array.ConvertAll(temp_str, s => int.Parse(s));
            for(int i=0; i<temp_int.Length; i++)
            {
                Console.WriteLine(temp_int[i]);   //Printing After Conversion.............
            }
            Console.ReadKey();
        }
    }
}

We use the Array.Convertall() method in the above code and pass the array. We are parsing the string array into an int array using int.Parse().

Output:

1000
2000
3000

Use LINQ’s Select() Method to Convert String Array to Int Array in C#

We can use the next method to convert a string array to an int array by passing the int.Parse() method to the LINQ’s Select() method. We’ll also need to call the ToArray() method to get the array.

Let’s see the implementation. Learn more about LINQ here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace Array_of_String_to_integer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("===================Using LINQ=======================================");
            // Method Using LINQ.........................
            //We can also pass the int.Parse() method to LINQ's Select() method and then call ToArray to get an array.
            string[] temp_str = new string[] { "1000", "2000", "3000" };
            int[] temp_int1 = temp_str.Select(int.Parse).ToArray();

            for (int i = 0; i < temp_int1.Length; i++)
            {
                Console.WriteLine(temp_int1[i]);   //Printing After Conversion.............
            }
            Console.ReadKey();
        }
    }
}

Output:

===================Using LINQ=======================================
1000
2000
3000
Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Csharp Array