您是否可以通过使用其构造函数来创建嵌套列表的深层副本? [英] Can you create deep copy of nested lists through use of it's constructor?
本文介绍了您是否可以通过使用其构造函数来创建嵌套列表的深层副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道您可以将列表对象作为列表构造函数的参数,以复制 1个列表到另一个列表.
I know you can give a list object as parameter of list constructor to copy 1 list to the other.
这样做,如果使用的类型是一次简单的内置操作(究竟是哪个?),则可以创建一个深层副本.例如类型 string :
By doing so, you can create a deep copy if used types are simple build-in once (which precisely?). For example of type string:
List<string> testList = new List<string>();
List<string> testListCopy = new List<string>(testList );
如果使用嵌套列表,是否可以创建深层副本? :
Can you create a deep copy if you work with nested Lists? :
List<List<string>> testList = new List<List<string>>();
List<List<string>> testListCopy = new List<List<string>>(testList );
推荐答案
List<T>
构造函数不会为您执行此操作,但是您可以自己使用以下方法进行操作:
The List<T>
constructor won't do this for you, but you can do it yourself with:
List<List<string>> testList = new List<List<string>>();
List<List<string>> testListCopy = new List<List<string>>(testList.Select(x => x.ToList()));
甚至是等效的,
List<List<string>> testListCopy = testList.Select(x => x.ToList()).ToList();
这篇关于您是否可以通过使用其构造函数来创建嵌套列表的深层副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文