How to convert a list of strings to list of int in C#

2 Answers

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        List<string> list = new List<string> { "1", "2", "3", "4", "5" };
        
        List<int> result = list.Select(int. Parse). ToList();
        
        Console.WriteLine(String.Join(", ", result)); 
    }
}
 
 
 
 
/*
run:
 
1, 2, 3, 4, 5
 
*/

 



answered Jul 29, 2023 by avibootz
edited Jul 29, 2023 by avibootz
0 votes
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        List<string> list = new List<string> { "1", "2", "3", "4", "5" };
        
        List<int> result = list.ConvertAll(int.Parse);
        
        Console.WriteLine(String.Join(", ", result)); 
    }
}
 
 
 
 
/*
run:
 
1, 2, 3, 4, 5
 
*/

 



answered Jul 29, 2023 by avibootz
edited Jul 29, 2023 by avibootz

Related questions

3 answers 224 views
224 views asked Sep 13, 2020 by avibootz
1 answer 97 views
1 answer 142 views
142 views asked Feb 12, 2016 by avibootz
1 answer 172 views
1 answer 206 views
1 answer 150 views
...