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
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
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);
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 StringCollection
s. 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
you can cast as array and use Union, please note this will also remove duplicates
var resultCollection = collection1.Cast<string>().Union(collection2.Cast<string>())
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();