If you need to convert a collection (for example List of string) to string separated with the Comma as delimiter , you can either use the string.join method or aggregate method as shwon below.
How to convert List<string> to string with comma delimiter in C# ?
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Xml.Linq; namespace ACCode { class Program { static void Main(string[] args) { List<string> movieNames = new List<string>(); movieNames.Add("Theri"); movieNames.Add("Kabali"); movieNames.Add("Bahubali 2"); // option 1 - using the string.join method var output1 = string.Join(", ", movieNames.ToArray()); // option 2 - using the Lambda expression / aggregate method defined in System.Linq namespace. var output2 = movieNames.Aggregate((a, b) => a + ", " + b); Console.WriteLine(output1); Console.WriteLine(output2); Console.ReadLine(); } } }
Leave a Reply