copy lists

  • Thread starter Thread starter Tem
  • Start date Start date
T

Tem

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

List<int> intList1 = new List<int>();
List<int> intList2 = 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
 
Tem said:
When I tried to add new items to a copied list, it affects both lists.

List<int> intList1 = new List<int>();
List<int> intList2 = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top