C# 程式來克隆或複製一個列表
要複製或克隆 C# 列表,首先設定列表。
List < string > myList = new List < string > ();
myList.Add("One");
myList.Add("Two");現在宣告一個字串陣列,並使用 CopyTo() 方法進行復制。
string[] arr = new string[10]; myList.CopyTo(arr);
讓我們看看將列表複製到一維陣列的完整程式碼。
示例
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List < string > myList = new List < string > ();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");
Console.WriteLine("First list...");
foreach(string value in list1) {
Console.WriteLine(value);
}
string[] arr = new string[20];
myList.CopyTo(arr);
Console.WriteLine("After copy...");
foreach(string value in arr) {
Console.WriteLine(value);
}
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP