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();

}

}

}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...