首页 > 技术文章 > C# deep copy List

argenbarbie 2018-12-21 16:06 原文

https://stackoverflow.com/questions/14007405/how-create-a-new-deep-copy-clone-of-a-listt

 

1. copy list of object

You need to create new Book objects then put those in a new List:

List<Book> books_2 = books_1.Select(book => new Book(book.title)).ToList();

Update: Slightly simpler... List<T> has a method called ConvertAll that returns a new list:

List<Book> books_2 = books_1.ConvertAll(book => new Book(book.title));

2. copy list of primitive types (string)

List<string> a = new List<string>{"C","P"};

var b = new List<string>(a);

推荐阅读