Sabari M Sabari M
Updated date Jan 19, 2023
In this blog, we will learn how to convert a comma-separated string into a List in C#

We can easily convert a comma-separated string into a List in C# using String.Split() method.

In the below code snippet, String.Split() method splits a comma-separated string into substrings based on the characters in an array, then you can convert the arrays into a list as shown below. You have to add the reference System.Linq for the method ToList().

using System;
using System.Linq;
using System.Collections.Generic;

namespace ConvertCommSepStringToList
{
    class Program
    {
        static void Main()
        {
            // Comma-seperated String
            string colorStr = "Red,Blue,White,Pink,Yellow";

            // Split a string into substrings based on the ","
            List<string> colors = colorStr.Split(',').ToList();

            // Display the element
            foreach (string color in colors)
                Console.WriteLine(color);            
            Console.ReadKey();
        }
    }
}

Output:

Red
Blue
White
Pink
Yellow

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!