In my C# code, I have a array of objects. And many of these objects are referenced in another class. If Array.sort method is used to somehow sort this array of objects, then will it affect those references? Is it same for the arrays and lists?
+6
A:
No it won't affect anything. The important thing to realise is that you don't actually have an array of objects. You have an array of references. So if you have objects A and B, then the array may contain references to A and B. When you sort the array, those references may change order - but the objects themselves don't get changed. Anything else which had a reference to the objects won't see any change just because you're shuffling the references.
To put it another way: suppose you have a shopping list of items you want to get. You can sort the shopping list to put it in some efficient order for shopping. That doesn't change where the items are, and it doesn't change anyone else's shopping list.
Jon Skeet
2010-04-28 04:31:53
+1: Can't think of a much better analogy.
Dan Tao
2010-04-28 04:34:54
+1: An Excellent Example...
The King
2010-04-28 04:47:05