10

This code snippet is from C# 2010 for Dummies. What confuses me is that when using the Array.Sort() method, both my copy of the array (sortedNames) and the original array (planets) get sorted, even though it only calls the Sort method on sortedNames.

It doesn't matter which array the second foreach loop references, the output is the same.

static void Main(string[] args)
{
    Console.WriteLine("The 5 planets closest to the sun, in order: ");
    string[] planets = new string[] { "Mercury","Venus", "Earth", "Mars", "Jupiter"};
    foreach (string planet in planets)
    {
        Console.WriteLine("\t" + planet);
    }
    Console.WriteLine("\nNow listed alphabetically: ");


    string[] sortedNames = planets;
    Array.Sort(sortedNames);

    foreach (string planet in planets)
    {
        Console.WriteLine("\t" + planet);
    }
}
1
  • 12
    sortedNames and planets refer to the same object Commented Nov 29, 2014 at 1:39

2 Answers 2

22

Both sortedNames and planets refer to the same array. Basically both variables point to the same location in memory, so when you call Array.Sort on either variable, the changes to the array are reflected by both variables.

Since arrays in C# are reference types, both sortedNames and planets "point" to the same location in memory.

Contrast this with value types, which hold data within their own memory allocation, instead of pointing to another location in memory.

If you wanted to keep planets intact, you could use create a brand new array, then use Array.Copy to fill the new array with the contents of planets:

/* Create a new array that's the same length as the one "planets" points to */
string[] sortedNames = new string[planets.Length];

/* Copy the elements of `planets` into `sortedNames` */
Array.Copy(planets, sortedNames, planets.Length);

/* Sort the new array instead of `planets` */
Array.Sort(sortedNames);

Or, using LINQ you could use OrderBy and ToArray to create a new, ordered array:

string[] sortedNames = planets.OrderBy(planet => planet).ToArray();

Some resources that might help with value types and reference types:

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

Comments

0

Alternatively you could use Array.Clone to avoid having to first create a new array. Then sort.

string[] sortedNames = (string[]) Array.Clone(planets);
Array.Sort(sortedNames);

See also this other discussion Difference between the System.Array.CopyTo() and System.Array.Clone()

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.