将列表复制到另一个列表(新列表不应引用旧列表) [英] Copying a list to another (new list should not reference to old list)

查看:92
本文介绍了将列表复制到另一个列表(新列表不应引用旧列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们



我想在C#中将列表复制到另一个。问题是像copyto这样的现有方法会从另一个列表创建一个新列表。但是,如果我对新列表进行一些更改,旧列表也会发生变化。它因为它们的引用类型行为。如果有任何方法(除了foreach循环)从另一个列表创建一个不会引用同一个对象的新列表?

i认为(AddRange)是一样的!

即使我的列表中的元素是引用类型,不仅值类型

Hi , guys

I want to copy a list to another in C#. The problem is that existing methods like copyto creates a new list from another list. But if I make some changes to new list the old list also gets changed. Its because of their reference type behaviour. If there any way (apart from foreach loop) to create a new list from another list which will not reference the same object ?
i think the (AddRange) is the same !
even if elements of my list are reference types not only value type

推荐答案

试试这个:



try this:

List<int32> original = new List<int32>();
original.Add(1);
original.Add(2);

List<int32> copy = new List<int32>(original);
original.Add(3);


这篇关于将列表复制到另一个列表(新列表不应引用旧列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆