1

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?

0

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

5

No, it will not affect references in other classes. Same for lists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.