Take the 2-minute tour ×

I need the above feature because I can only store StringCollection to Settings, not List of strings.

How does one convert List into StringCollection?

share|improve this question

2 Answers 2

up vote 16 down vote accepted

How about:

StringCollection collection = new StringCollection();

Alternatively, avoiding the intermediate array (but possibly involving more reallocations):

StringCollection collection = new StringCollection();
foreach (string element in list)

Converting back is easy with LINQ:

List<string> list = collection.Cast<string>().ToList();
share|improve this answer
Just wondering, why would avoiding intermediate array be recommended? –  l46kok Aug 17 '12 at 7:43
@l46kok: All other things being equal, avoiding extra intermediate copies is always recommended. In this case, however, it's not obvious which approach is more efficient (due to the extra reallocations) and probably not important anyway, given the typical size of a settings collection. –  Marcelo Cantos Aug 17 '12 at 7:45
@MarceloCantos: Always? I wouldn't go with that. The first code is clearly simpler, and given that (as you say) it's probably not important, I'd stick with the possibly-less-efficient-but-more-obvious approach. –  Jon Skeet Aug 17 '12 at 7:55
@JonSkeet: You're right. I included that in "All other things being equal," but I wasn't as clear as I should have been. –  Marcelo Cantos Aug 17 '12 at 9:45

Use List.ToArray() which will convert List to an Array which you can use to add values in your StringCollection.

StringCollection sc = new StringCollection();

//use sc here.

Read this

share|improve this answer
Why are you doing string[] str = mylist.ToArray(); when you could just do sc.AddRange(mylist.ToArray());? –  dtsg Aug 17 '12 at 7:45
@john: To tell him what would mylist.ToArray() returns and AddRange() takes. –  Nikhil Agrawal Aug 17 '12 at 7:46
Isn't that obvious by using .ToArray();? –  dtsg Aug 17 '12 at 7:49
@john: Happy. Now remove downvote. –  Nikhil Agrawal Aug 17 '12 at 7:51
@john What a waste of voting rights. Jon's answer and his answer are practically same and still he gets 2 downvotes... –  Amit Mittal Aug 17 '12 at 7:58

Your Answer


