废话不多说,看代码:
方法一:
list<string> t = new list<string>(); //original
list<string> t2 = new list<string>(t.toarray()); // copy of t
方法二:
it is a one liner using linq.
list<string> list1 = new list<string>();
list<string> list2 = new list<string>();
// 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> x = new list<x>();
for (int i = 0; i < 100; i++)
x.add(new x { id = i, name = string.format("x_{0}", i.tostring()) });
// copy x to y
list<y> y = new list<y>(x.convertall<y>(e => { return new y { id = e.id, name = e.name }; }));
debug.assert(x.count == y.count);
}
}
}
更多 C# List 复制克隆副本相关文章请关注PHP中文网!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号