Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将System.Collections.Specialized.StringCollection类型转换为string[]_C#_C# 4.0 - Fatal编程技术网

C# 如何将System.Collections.Specialized.StringCollection类型转换为string[]

C# 如何将System.Collections.Specialized.StringCollection类型转换为string[],c#,c#-4.0,C#,C# 4.0,我的类库中的某些函数接受string[]作为参数 我想将我的System.Collections.Specialized.StringCollection转换为string[] 是否可以使用某个单行程序或我必须使用循环创建数组?用于将内容复制到字符串数组这在所有.Net框架中都受支持。 System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection()

我的类库中的某些函数接受
string[]
作为参数

我想将我的
System.Collections.Specialized.StringCollection
转换为
string[]

是否可以使用某个单行程序或我必须使用循环创建数组?

用于将内容复制到字符串数组这在所有.Net框架中都受支持。

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);
试试这个

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>();
System.Collections.Specialized.StringCollection strs=new System.Collections.Specialized.StringCollection();
标准添加(“废话”);
标准添加(“废话”);
标准添加(“废话”);
字符串[]strArr=strs.Cast().ToArray();
这就做到了:

System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
/*sc.Add("A");
sc.Add("B");*/
string[] asArray = sc.Cast<string>().ToArray();
System.Collections.Specialized.StringCollection sc=new System.Collections.Specialized.StringCollection();
/*sc.Add(“A”);
sc.Add(“B”)*/
字符串[]asArray=sc.Cast().ToArray();

免责声明:我不知道这是什么性能特征。

strs.Cast().ToArray()就足够了。您不必在
ToArray
@Habib中再次强制转换为字符串:
.ToArray()
.ToArray()
之间没有区别,除了输入时的击键数。