將集合元素複製到 C# 中的陣列
將集合元素複製到陣列的程式碼如下 −
示例
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("
Array...");
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("
Array...");
foreach(string str in strArr){
Console.WriteLine(str);
}
}
}輸出
這將產生以下輸出 −
Collection.... One Two Three Four Five Array... One Two Three Four Five
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP