0 Comments

If you want to concatenate all items in a generic List<string> in C# you can use the following code:

 

List<string> itemsInGenericList = new List<string>
    {
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4"
    };

// Convert generic list of strings to an array of strings.
string[] itemsInArray = itemsInGenericList.ToArray();

// Convert an array of strings to one string.
string itemsInString = string.Join(Environment.NewLine, itemsInArray);
Console.WriteLine(itemsInString);

// Convert an array of strings to a generic list of strings.
itemsInGenericList = new List<string>(itemsInArray);

 

Result

Item 1

Item 2

Item 3

Item 4

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts