Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<string> to StringCollection

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

How does one convert List into StringCollection?

like image 292
l46kok Avatar asked Aug 17 '12 07:08

l46kok


2 Answers

How about:

StringCollection collection = new StringCollection();
collection.AddRange(list.ToArray());

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

StringCollection collection = new StringCollection();
foreach (string element in list)
{
    collection.Add(element);
}

Converting back is easy with LINQ:

List<string> list = collection.Cast<string>().ToList();
like image 165
Jon Skeet Avatar answered Oct 16 '22 17:10

Jon Skeet


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

StringCollection sc = new StringCollection();
sc.AddRange(mylist.ToArray());

//use sc here.

Read this

like image 26
Nikhil Agrawal Avatar answered Oct 16 '22 17:10

Nikhil Agrawal