Reputation: 1291
Usually, joining a List with commas is easy using string.Join(). However, coming across a StringCollection today, string.Join() outputs "System.Collections.Specialized.StringCollection" output instead of a comma-separated string.
[TestMethod]
public void TestJoin()
{
StringCollection stringCollection = new StringCollection() { "Denis", "Jason", "Shawn" };
// Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException:
// 'Assert.AreEqual failed. Expected:<Denis, Jason, Shawn>. Actual:<System.Collections.Specialized.StringCollection>.'
Assert.AreEqual("Denis, Jason, Shawn", string.Join(", ", stringCollection));
}
How do we Join() a StringCollection?
Upvotes: 0
Views: 559
Reputation: 1291
It is possible to convert the StringCollection to a List collection first: https://stackoverflow.com/a/844420/4682228.
var commaSeparatedList = string.Join(", ", stringCollection.Cast<string>());
Upvotes: 4