首頁  >  文章  >  後端開發  >  用於克隆或複製清單的 C# 程序

用於克隆或複製清單的 C# 程序

WBOY
WBOY轉載
2023-09-02 14:17:02821瀏覽

用于克隆或复制列表的 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);
      }
   }
}
#

以上是用於克隆或複製清單的 C# 程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除