1

How to combine / merge two StringCollection in C#

var collection1 = new StringCollection () { "AA", "BB", "CC" };
var collection2 = new StringCollection () { "DD", "EE", "FF" };
var resultCollection = collection1 + collection2 ; // TODO
                               
Rahul Uttarkar
  • 3,367
  • 3
  • 35
  • 40
  • 3
    StringCollection is an obsolete type, you shouldn't be using it *at all*. – Servy Oct 14 '20 at 18:09
  • I would agree that just about any generic collection like List would be better, and I had never heard of StringCollection before, but if it's an obsolete type why would it still be supported in .NET 5.0RC1 https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringcollection?view=netcore-3.1 Is it being kept around just to support legacy code? Maybe Mark is working on legacy code and needs to continue using StringCollection. – TJ Rockefeller Oct 14 '20 at 19:17
  • 1
    @TJRockefeller If you work on the .NET world, you should know that all code is made retrocompatible all the way to .NET 1.0. So it is still going to exist as long as Microsoft doesn't reverse that decision and finally obsolete so much **** :) – Camilo Terevinto Oct 14 '20 at 19:31

4 Answers4

2

You can copy all to an array like this

    var collection1 = new StringCollection() { "AA", "BB", "CC" };
    var collection2 = new StringCollection() { "DD", "EE", "FF" };

    var array = new string[collection2.Count + collection1.Count];

    collection1.CopyTo(array, 0);
    collection2.CopyTo(array, collection1.Count);
Beingnin
  • 2,288
  • 1
  • 21
  • 37
1

If you still want a string collection you can just use AddRange

var collection1 = new StringCollection () { "AA", "BB", "CC" };
var collection2 = new StringCollection () { "DD", "EE", "FF" };
var resultCollection = new StringCollection();
resultCollection.AddRange(collection1.Cast<string>.ToArray());
resultCollection.AddRange(collection2.Cast<string>.ToArray());

Seems odd that StringCollection doesn't have any direct support for adding other StringCollections. If efficiency is a concern, Beingnin's answer is probably more efficient than the answer here, and if you still need it in a StringCollection you can take the array that is generated and use AddRange to add that array of strings to a new StringCollection

TJ Rockefeller
  • 3,178
  • 17
  • 43
  • "is probably more efficient" -> it is definitively more efficient. It's one loop over each collection versus two loops in your answer. Also, why not just call `AddRange` on one of the existing collections? – Camilo Terevinto Oct 14 '20 at 17:41
  • @CamiloTerevinto it's not clear what the intent is for the string collections here. If you call `AddRange` on an existing collection it will modify that collection instead of giving you a new collection. The question does not suggest that it would be okay to mutate one of the original collections, so this combines the two collections into a new collection without changing the original collections. – TJ Rockefeller Oct 14 '20 at 19:04
0

you can cast as array and use Union, please note this will also remove duplicates

var resultCollection = collection1.Cast<string>().Union(collection2.Cast<string>())
ssilas777
  • 9,672
  • 4
  • 45
  • 68
-2

you can occupy List instead of StringCollection ...

        var collection1 = new List<string>() { "AA", "BB", "CC" };
        var collection2 = new List<string>() { "DD", "EE", "FF" };
        var resultCollection = collection1.Concat(collection2).ToList();