1
static int[] scores = new int[100];
static int[] scorescopy;
public static int orderscores()
{
   scorescopy = scores;
    Array.Sort(scorescopy);
    int sortingtoolb = 0;
    return 0;
}

I am trying to get a copy of my initial array and then trying to sort that copy. However, when I use the Array.Sort() function, my first array keeps on being sorted as well, but I would like to preserve it. I tried taking away the new declaration on the scorescopy and that did not effect the result.

Also, is there a way to keep my unused variables within the array as null? (if I am not using all parts of it, I get a bunch of 0's in the beginning of the array).

I am using Visual Studio Express 2012 for Windows 8 on a system running Windows 8.1 Pro.

2
  • 1
    You need to understand the difference between reference types and value types, and specifically what this means for assignment. Chapter 1 of most C# books will cover this. Commented Mar 20, 2014 at 16:50
  • It was actually covered in Chapter 10 in my book... but since I had forgotten that it was called cloning, I failed to find the answer on my own through it and the internet. Commented Mar 20, 2014 at 17:10

3 Answers 3

6

An array, when assigned, only copies a reference to the same array in memory. You need to actually copy the values for this to work:

public static int orderscores()
{
    scorescopy = scores.ToArray(); // Using LINQ to "cheat" and make the copy simple
    Array.Sort(scorescopy);
    int sortingtoolb = 0;
    return 0;
}

Note that you can do this without LINQ via:

scorescopy = new int[scores.Length];
Array.Copy(scores, scorescopy, scores.Length);
//... rest of your code
Sign up to request clarification or add additional context in comments.

Comments

3

The expression scorescopy = scores; duplicate the handle to the array.

if you want to create a copy of the array items you should change that line to:
scores.copyTo(scorescopy,0);

You still need to make sure scorecopy has enough room to store the items.
so you also need this expression: static int[] scorescopy = new int[scores.Length];

and now your code should like this:

static int[] scores = new int[100];
static int[] scorescopy = new int[scores.Length];

public static int orderscores()
{
    scores.copyTo(scorescopy,0);
    Array.Sort(scorescopy);
    int sortingtoolb = 0;
    return 0;
}

2 Comments

Note that this will raise an exception if scorescopy isn't allocated first.
true - will fix that asap :)
2

You are getting a pointer to the same array, you want a clone:

scorescopy = (int [])scores.Clone();

3 Comments

@ReedCopsey Yes it is valid C#. Well apart from it should be a capital C on clone();
@ReedCopsey Then don't just say its invalid. Say what is wrong. Saying it is invalid implies it is totally incorrect.
Thank you for the extremely simple solution! I had forgotten about the clone function.

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.