0

I have a 3x3 boolean 2D array in my C# code. I want to rotate the values of the array around the [1,1] axis (the middle). I tried copying the values into a new 2D array then overriding the old 2D array by the new values from the new one, but for some reason the new 2D array always seems to change its [1,0] value from 'true' to 'false' even though I don't override it in any way inside my code, it just seems to change itself alone.

Here is the piece of code i'm having a problem with.

bool[,] OldGrid = blockGrid;

        print(OldGrid[0, 1] + " " + blockGrid[0, 1]);

        blockGrid[0, 0] = OldGrid[0, 2];
        blockGrid[0, 1] = OldGrid[1, 2];
        blockGrid[0, 2] = OldGrid[2, 2];

        print(OldGrid[0, 1] + " " + blockGrid[0, 1]);

The first print method always returns 'TRUE TRUE'

While the second one always returns 'FALSE FALSE'

Although it should return 'TRUE FALSE'

  • 4
    Did you think `bool[,] OldGrid = blockGrid;` copies the array? It does not. – Sweeper Jul 05 '20 at 13:06
  • Oh... what does it do then? And how to copy the array? – Ahmed Mahmoud Jul 05 '20 at 13:09
  • Arrays are reference types. This means you have one array with two references: `OldGrid` and `blockGrid`. – Zohar Peled Jul 05 '20 at 13:09
  • See : [Reference Types](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types). Related : [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/q/5057267/327083) – J... Jul 05 '20 at 13:10
  • Ah, got you. So if I wanted to copy the array into a new unique one, should I like copy the values each individually? – Ahmed Mahmoud Jul 05 '20 at 13:12
  • Does this answer your question? [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) – Pavel Anikhouski Jul 05 '20 at 14:18
  • @AhmedMahmoud Yes, but only after having first *allocated a new array* to copy the values into. – J... Jul 05 '20 at 22:49
  • @PavelAnikhouski It answers half of my question... I indeed didn't know about the difference, but my question also asks for the Clone() function which I didn't know existed to be honest. – Ahmed Mahmoud Jul 18 '20 at 10:41

1 Answers1

2

this line of code:

bool[,] OldGrid = blockGrid;

does not create a new copy of the array, instead it makes a new array referencing to the original blockGrid. So later when you use blockGrid[0, 1] = OldGrid[1, 2]; you override the value in OldGrid[0,1] with that of [1,2] by reference.

The method that you are looking for is making a clone bool[,] OldGrid = (bool[,])blockGrid.Clone();

JaHoff
  • 196
  • 5
  • 1
    Thanks so much for your answer, but there should be a small change in the method. `bool[,] OldGrid = (bool[,])blockGrid.Clone();` – Ahmed Mahmoud Jul 05 '20 at 13:26
  • Hey, I'm glad it works for you! Your suggestion is correct, I did indeed make a mistake in my suggestion. I edited my answer to incorporate your change – JaHoff Jul 05 '20 at 14:20