C# List 复制克隆副本

来源:这里教程网 时间:2026-02-21 13:04:12 作者:

废话不多说,看代码:

方法一:

      list t = new list(); //original 

      list t2 = new list(t.toarray()); // copy of t



方法二:




it is a one liner using linq.

      list list1 = new list();
      list list2 = new list();

      // this will copy all the items from list 1 to list 2
      list1.foreach(i => list2.add(i));




方法三:
using system;
using system.linq;
using system.collections.generic;
using system.diagnostics;

namespace delegates
{
  class x
  {
    public int id { get; set; }
    public string name { get; set; }
  }

  class y
  {
    public int id { get; set; }
    public string name { get; set; }
  }

  class program
  {
    static void main(string[] args)
    {
      list x = new list();
      for (int i = 0; i         x.add(new x { id = i, name = string.format("x_{0}", i.tostring()) });
      // copy x to y
      list y = new list(x.convertall(e => { return new y { id = e.id, name = e.name }; }));
      debug.assert(x.count == y.count);
    }

  }
}

更多 C# List 复制克隆副本相关文章请关注PHP中文网!

相关推荐