Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<string> to List<int> in C#

Tags:

I want to convert a List<string> to a List<int>.

Here is my code:

void Convert(List<string> stringList) {     List<int> intList = new List<int>();       for (int i = 0; i < stringList.Count; i++)      {           intList.Add(int.Parse(stringList[i]));       } ) 
like image 717
user2939293 Avatar asked Nov 02 '13 08:11

user2939293


People also ask

How do I convert a string list to an int list?

This basic method to convert a list of strings to a list of integers uses three steps: Create an empty list with ints = [] . Iterate over each string element using a for loop such as for element in list . Convert the string to an integer using int(element) and append it to the new integer list using the list.

How do I turn a list into an int?

Use int() function to Convert list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.

How do I convert a list of strings to a list of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);


1 Answers

Instead of using LINQ you can use List<T>.ConvertAll<TOutput>(...)

List<int> intList = stringList.ConvertAll(int.Parse); 
like image 128
heijp06 Avatar answered Oct 07 '22 22:10

heijp06