Search code examples
c#.netstringlist

Convert a list to a string in C#


How do I convert a list to a string in C#?

When I execute toString on a List object, I get:

System.Collections.Generic.List`1[System.String]


Solution

  • Maybe you are trying to do:

    string combinedString = string.Join( ",", myList.ToArray() );
    

    You can replace , with whatever character you want to split the elements in the list by.

    Also, as mentioned in the comments you could also do:

    string combinedString = string.Join( ",", myList);
    

    Reference:

    Join<T>(String, IEnumerable<T>)

    Concatenates the members of a constructed IEnumerable collection of type String, using the specified separator between each member.