Convert a list into a comma-separated string
Solution 1
Enjoy!
Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));
First Parameter: ","
Second Parameter: new List<uint> { 1, 2, 3, 4, 5 })
String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.
Solution 2
You can use String.Join method to combine items:
var str = String.Join(",", lst);
Solution 3
Using String.Join
:
string.Join<string>(",", lst);
Using LINQ aggregation:
lst .Aggregate((a, x) => a + "," + x);
Solution 4
If you have a collection of ints:
List<int> customerIds= new List<int>() { 1,2,3,3,4,5,6,7,8,9 };
You can use string.Join
to get a string:
var result = String.Join(",", customerIds);
Enjoy!
Solution 5
Follow this:
List<string> name = new List<string>();
name.Add("Latif");
name.Add("Ram");
name.Add("Adam");
string nameOfString = (string.Join(",", name.Select(x => x.ToString()).ToArray()));
Related videos on Youtube

skjcyber
My name is Soumitra Kumar Jana and I am currently working as a Cloud Architect specializing in Microsoft Azure Cloud. My area of expertise includes - Cloud Platform & Infrastructure | Automation | DevOps | Azure Security | Infrastructure-as-a-Code | .NET Development A Software Developer having sound knowledge of all aspects of SDLC - Requirement Gathering, Development, Automated Testing, Build and Automated Deployment. Experienced in: C#, PowerShell, Microsoft Azure, SQL Server, REST API, Azure DevOps, Terraform, Agile Methodologies, Automation Testing, Virtualization (Application, Desktop, Hyper, User State) etc. The three chief virtues of a programmer are: Laziness, Impatience and Hubris - Larry Wall, Creator of the Perl programming language.
Updated on June 11, 2021Comments
-
skjcyber over 1 year
My code is as below:
public void ReadListItem() { List<uint> lst = new List<uint>() { 1, 2, 3, 4, 5 }; string str = string.Empty; foreach (var item in lst) str = str + item + ","; str = str.Remove(str.Length - 1); Console.WriteLine(str); }
Output:
1,2,3,4,5
What is the most simple way to convert the
List<uint>
into a comma-separated string?-
asawyer almost 10 years
String.Join
is all you need. -
Mithrandir almost 10 years
var csvString = String.Join(",", lst);
should do it. -
Tim Schmelter almost 10 yearsFor anyone who wants to reopen this, if it's not too localized it's a duplicate: stackoverflow.com/questions/799446/…
-
Peter Mortensen over 1 yearSimilar (from 2008): How can I join int[] to a character-separated string in .NET?
-
-
Anton almost 9 yearsIn .NET 3.5 and below you have to explicitly convert your list to array with
lst.ToArray()
, as there is no direct overload there yet. -
Hari almost 8 yearsI have list of type int32. when I use aggregate function you mentioned, it says "Cannot convert lambda expression to delegate type 'System.Func<int,int,int>' because some of the return types in the block are not implicitly convertible to the delegate return type " and "Cannot implicitly convert type 'string' to 'int'"
-
bets over 5 years@Hari You must convert it to string values before you Aggragate to string. So you can do something like this: list.Select(x => string.Format("{0}:{1}", x.Key, x.Value)).Aggregate((a, x) => a+ ", " + x);
-
Rani Radcliff about 3 yearsthis does not work for me. It produces the name of the collection rather than the objects in the list.
-
Peter Mortensen over 1 yearThe question was about C#, not Java.
-
Peter Mortensen over 1 yearThis is essentially identical to the accepted answer from 2013.
-
Peter Mortensen over 1 yearFrom what C# version is this valid?
-
Hans Kesting over 1 yearIs this Javascript? (the question is about C#)