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

WBOY
WBOY 转载
2023-09-02 14:17:02 472浏览

用于克隆或复制列表的 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,如有侵犯,请联系admin@php.cn删除