Convert a String of Integers to a List of Integers using C# and LINQ
This C# code snippet takes a comma delimited string of integers, and converts in to a List<int> variable. It uses LINQ to perform the conversion from List<string> to List<int>.
Make sure you include the following using statements.
using System.Collections.Generic;
using System.Linq;
Here is the actual code that converts the variable "source" into a list of integers called "result".
string source = "1,2,3,4,5";
List<int> result = new List<string>(source.Split(',')).ConvertAll(i => int.Parse(i));
Written By: mycodeshare on April 07, 2009 at 16:19
Submit a comment, suggestion, or additional information about this snippet
If you login or register,
you'll be able to post a comment or provide feedback about this snippet.