---
title: "How to create a comma-separated list from IList\"string\" or IEnumerable\"string\" in C#?"  
description: "How to create a comma-separated list from IList\"string\" or IEnumerable\"string\" in C#?"  
author: "Steilla Mitchel"  
published: 2023-07-12  
updated: 2023-07-13  
canonical: https://www.mindstick.com/forum/159056/how-to-create-a-comma-separated-list-from-ilist-string-or-ienumerable-string-in-c-sharp  
category: "c#"  
tags: ["c#", "string", "list"]  
reading_time: 2 minutes  

---

# How to create a comma-separated list from IList"string" or IEnumerable"string" in C#?

How to create a comma-separated list from IList<[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)> or IEnumerable<string> in C#?

## Replies

### Reply by Aryan Kumar

Sure, here is how to create a comma separated list from `IList<string>` or `IEnumerable<string>` in C#:

C#

```plaintext
using System;

public class CreateCommaSeparatedList
{
    public static string CreateCommaSeparatedList(IEnumerable<string> strings)
    {
        // Create a StringBuilder to hold the comma separated list.
        StringBuilder sb = new StringBuilder();

        // Iterate over the strings and append them to the StringBuilder, separated by commas.
        foreach (string string in strings)
        {
            sb.Append(string);
            sb.Append(",");
        }

        // Remove the trailing comma.
        sb.Length -= 1;

        // Return the comma separated list.
        return sb.ToString();
    }

    public static void Main(string[] args)
    {
        // Create a list of strings.
        List<string> strings = new List<string> { "hello", "world", "foo", "bar" };

        // Create a comma separated list from the strings.
        string commaSeparatedList = CreateCommaSeparatedList(strings);

        // Print the comma separated list.
        Console.WriteLine(commaSeparatedList); // This will print "hello,world,foo,bar"
    }
}
```

This code first creates a list of strings. Then, it calls the `CreateCommaSeparatedList()` method to create a comma separated list from the strings. Finally, the code prints the comma separated list to the console.

The `CreateCommaSeparatedList()` method takes an `IEnumerable<string>` as a parameter and returns a string. The method first creates a `StringBuilder` to hold the comma separated list. Then, the method iterates over the strings in the `IEnumerable<string>` and appends them to the `StringBuilder`, separated by commas. Finally, the method removes the trailing comma from the `StringBuilder` and returns the comma separated list.


---

Original Source: https://www.mindstick.com/forum/159056/how-to-create-a-comma-separated-list-from-ilist-string-or-ienumerable-string-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
