C# Tips and Tricks #13 – Create Comma seperated string from a List.

Assume that you have a List of string and you wish you to get the items of the list into a comma separated string , you will be able to it easily do with the help of string.join method.

How to generate a comma seperated string from a list in C# ?

Assume that the list contains three items Senthil , Kumar , CredAbility as shown below.

IList&lt;string&gt; lst = new List<string>{"Senthil","Kumar","CredAbility"};

To get the comma seperated string of all the items in the list , use the string.Join as shown below.

string commaseperatedstring = string.Join(",", lst);
image

Here’s the full code snippet that is used in this example.

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<string> lst = new List<string> { "Senthil", "Kumar", "CredAbility" };
            string commaseperatedstring = string.Join(",", lst);
            Console.WriteLine(commaseperatedstring);

            Console.ReadLine();
        }
    }
}
    

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...