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()
之间没有区别,除了输入时的击键数。