Home >Backend Development >C++ >How to Convert a List to a String in C#?
Issue: Attempting to convert a List object to a string using toString returns the type information instead of the list's contents.
Query: How to correctly convert a list to a string in C#?
Answer:
To convert a list to a string, you can use the string.Join() method along with the ToArray() or IEnumerable interface.
Solution 1:
string combinedString = string.Join(",", myList.ToArray());
This method separates the list elements with a comma. You can change the "," to any desired delimiter.
Solution 2 (alternative):
string combinedString = string.Join(",", myList);
This solution also works if the list contains strings.
Reference:
string.Join
The above is the detailed content of How to Convert a List to a String in C#?. For more information, please follow other related articles on the PHP Chinese website!