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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...