Home > Backend Development > C#.Net Tutorial > C# program to clone or copy a list

C# program to clone or copy a list

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-02 14:17:02
forward
930 people have browsed it

用于克隆或复制列表的 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");
Copy after login

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

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

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);
      }
   }
}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template