简体   繁体   English

用逗号加入 StringCollection

[英]Join StringCollection with commas

Usually, joining a List with commas is easy using string.Join().通常,使用 string.Join() 很容易用逗号连接列表。 However, coming across a StringCollection today, string.Join() outputs "System.Collections.Specialized.StringCollection" output instead of a comma-separated string.但是,今天遇到 StringCollection,string.Join() 输出“System.Collections.Specialized.StringCollection”而不是逗号分隔的字符串。

    [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?我们如何 Join() 一个 StringCollection?

可以先将 StringCollection 转换为 List 集合: https ://stackoverflow.com/a/844420/4682228。

var commaSeparatedList = string.Join(", ", stringCollection.Cast<string>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM