将Collection元素复制到C#中的数组
要将Collection元素复制到数组,代码如下-
示例
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<string> col = new Collection<string>();
col.Add("One");
col.Add("Two");
col.Add("Three");
col.Add("Four");
col.Add("Five");
col.Add("Six");
col.Add("Seven");
col.Add("Eight");
Console.WriteLine("Collection....");
foreach(string str in col){
Console.WriteLine(str);
}
string[] strArr = new string[10];
col.CopyTo(strArr, 2);
Console.WriteLine("\nArray...");
foreach(string str in strArr){
Console.WriteLine(str);
}
}
}输出结果
这将产生以下输出-
Collection.... One Two Three Four Five Six Seven Eight Array... One Two Three Four Five Six Seven Eight
示例
现在让我们来看另一个示例-
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main(){
Collection<string> col = new Collection<string>();
col.Add("One");
col.Add("Two");
col.Add("Three");
col.Add("Four");
col.Add("Five");
Console.WriteLine("Collection....");
foreach(string str in col){
Console.WriteLine(str);
}
string[] strArr = new string[10];
col.CopyTo(strArr, 3);
Console.WriteLine("\nArray...");
foreach(string str in strArr){
Console.WriteLine(str);
}
}
}输出结果
这将产生以下输出-
Collection.... One Two Three Four Five Array... One Two Three Four Five
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短