How to convert string array to integer array easily using LINQ?

There are times when you would have an array of string with only integer values and you may want to convert them to an integer array.

One technique to do it is to copy the array element one by one to an integer array with the int.tryparse or int.parse.

Below is another simple technique using LINQ to convert string array to integer array USING linq extension method

How to convert string array to integer array easily using LINQ?

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

namespace Ginktage

{

internal class Program

{

//How to Convert string array to integer array easily using LINQ ?

private static void Main(string[] args)

{

string [] AbundabtStrArray = new string[] { "98", "12", "82", "1" };

int[] AbundabtIntArray = AbundabtStrArray.Select(int.Parse).ToArray();

foreach (var item in AbundabtIntArray)

Console.WriteLine(item);

Console.ReadLine();

}

}

}
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

No Responses

Leave a Reply


x

Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...