Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert type System.Collections.Specialized.StringCollection to string[]

Tags:

c#

c#-4.0

Some functions in my class library accepts string[] as parameter.

I want to convert my System.Collections.Specialized.StringCollection to string[].

Is it possible with some one liner or I have to create array with loop?

like image 485
Annie Avatar asked Nov 15 '12 06:11

Annie


2 Answers

Use StringCollection.CopyTo(string[],index) to copy the contents to string array. This is supported in all .Net frameworks.

System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
sc.Add("Test");
sc.Add("Test2");

string[] strArray = new string[sc.Count];
sc.CopyTo(strArray,0);
like image 99
Habib Avatar answered Nov 08 '22 09:11

Habib


Try this

System.Collections.Specialized.StringCollection strs = new  System.Collections.Specialized.StringCollection();
strs.Add("blah"); 
strs.Add("blah"); 
strs.Add("blah"); 

string[] strArr = strs.Cast<string>().ToArray<string>();
like image 32
yogi Avatar answered Nov 08 '22 10:11

yogi