How to get a comma separated string from an array in C#?


We can get a comma-separated string from an array using String.Join() method.

Example: String.Join()
string[] animals = { "Cat", "Alligator", "Fox", "Donkey" };
var str = String.Join(",", animals);

In the same way, we can get a comma-separated string from the integer array.

Example: String.Join()
int[] nums = { 1, 2, 3, 4 };
var str = String.Join(",", nums); 

We can also get a comma separated string from the object array, as shown below.

Example: String.Join()
Person[] people = {
        new Person(){ FirstName="Steve", LastName="Jobs"},
        new Person(){ FirstName="Bill", LastName="Gates"},
        new Person(){ FirstName="Lary", LastName="Page"}
    };
 
var str = String.Join(",", people.Select(p => p.FirstName) );

Thus, we can easily get the string with comma separated or any other separator from the array in C#.