Links

 

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));
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.
Contact Us | Terms of Use