Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม 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);
      }
   }
}