C# How To: CSV to List or List to CSV

The following code snippet shows how to convert from a CSV string to a List using the string class Split method.


const string ColoursCsv = "red,blue,green,yellow";
                       
List<string> coloursList = ColoursCsv.Split(',').ToList();

The following code snippet shows how to convert from a list to a CSV string using the string class Join method.

List<string> coloursList = new List<string> {
    "red",
    "blue",
    "green",
    "yellow"
};

string coloursCsv = string.Join(",", coloursList);
Previous
Next Post »