Home  >  Article  >  Backend Development  >  C# program to clone or copy a list

C# program to clone or copy a list

WBOY
WBOYforward
2023-09-02 14:17:02823browse

用于克隆或复制列表的 C# 程序

To copy or clone a C# list, first set up a list.

List < string > myList = new List < string > ();
myList.Add("One");
myList.Add("Two");

Now declare an array of strings and copy using the CopyTo() method.

string[] arr = new string[10];
myList.CopyTo(arr);

Let’s see the complete code for copying a list to a one-dimensional array.

Example

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);
      }
   }
}

The above is the detailed content of C# program to clone or copy a list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete