473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

copy lists

Tem
When I tried to add new items to a copied list, it affects both lists.

List<intintList1 = new List<int>();
List<intintList2 = new List<int>();

intList1.Add(1);
intList2 = intList1;
intList2.Add(2);

how i can simply copy the data not reference the original list. this is very
confusing. are there other data types like this? how can i tell?

Tem

Jun 27 '08 #1
1 997
Tem wrote:
When I tried to add new items to a copied list, it affects both lists.

List<intintList1 = new List<int>();
List<intintList2 = new List<int>();

intList1.Add(1);
intList2 = intList1;
intList2.Add(2);

how i can simply copy the data not reference the original list. this is
very confusing. are there other data types like this? how can i tell?
That is how references work in .NET !

Try replace:

intList2 = intList1;

with:

intList2 = new List<int>(intList1);

Arne

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
0
by: Krasimir_Slaveykov | last post by:
Hello George, Monday, July 14, 2003, 11:46:06 AM, you wrote: Read this: http://www.mysql.com/doc/en/ANSI_diff_Sub-selects.html
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
3
by: Cybertof | last post by:
Hello, Can someone explain me what is the differences between these 2 lines of code : MyArrayList2 = MyArrayList; MyArrayList2 = MyArrayList.Clone(); MyArrayList/MyArrayList2 are aray...
6
by: Desmond Cassidy | last post by:
Hi, I'm sure this has been asked several times before but I'll risk it ;-) If I wish to save an Arraylist to another Arraylist and work on te original without affecting the contents of the new...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
3
by: timmy | last post by:
i make a copy of a list, and delete an item from it and it deletes it from the orginal as well, what the hell is going on?!?!?! #create the staff copy of the roster Roster2 = for ShiftLine in...
8
by: serge calderara | last post by:
Dear all, I have an treeview control with different node object, I would like to implement the Copy/Paste function of an object . For that I am using the folowing function to copy teh object to...
4
by: john | last post by:
Hey guys, Thank you for your input from the last topic. I tried referencing my c+ + book and googled the topic but i'am still confused a bit. I have another problem with that same code. I was...
1
by: bg_ie | last post by:
Hi there, In my application I have a Settings class which has two objects, userSettings and defaultSettings. When the application starts, I use a deep copy to set-up userSettings with the values...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.