So i have this:
List<CustomClass> list1 = new List<CustomClass>;
list1.Add(CustomClass);
list1.Add(CustomClass);
List<CustoClass> list2 = new List<CustomClass>;
list2 = list1; //the Count of list2 is equal to 2
list1.Clear(); //the Count of list2 is equal to 0
So when i clear list1, list2 is cleared as well. Why? I don't want that.
List<T> not copied...
Page 1 of 12 Replies - 933 Views - Last Post: 30 March 2009 - 01:20 PM
Replies To: List<T> not copied...
#3
Re: List<T> not copied...
Posted 30 March 2009 - 01:20 PM
The reason is that List<T> is a class and thus a reference type. When you assign list2 to be equal to list1 you actually point them towards the same reference instead of copying the list.
It should be easy to find some articles on reference and value types on the internet.
It should be easy to find some articles on reference and value types on the internet.
Page 1 of 1

New Topic/Question
Reply



MultiQuote




|