top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert List<string> to string with comma delimiter in C# ?

0 votes
337 views
How to convert List<string> to string with comma delimiter in C# ?
posted May 30, 2016 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
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(); 
        } 
    } 
}
answer May 30, 2016 by Shivaranjini
...