Blankman
Blankman

Reputation: 266978

Given a List<int> how to create a comma separated string?

Given a List<int> how to create a comma separated string?

Upvotes: 14

Views: 4470

Answers (2)

user243966
user243966

Reputation:

If it's going to be a large string, you may want to consider using the StringBuilder class because it is less memory intensive. It doesn't allocate memory each time you append another string, which yields performance improvements.

Upvotes: 0

dtb
dtb

Reputation: 217263

You can use String.Join:

List<int> myListOfInt = new List<int> { 1, 2, 3, 4 };

string result = string.Join<int>(", ", myListOfInt);

// result == "1, 2, 3, 4"

Upvotes: 33

Related Questions