c# - Array value seems to be different while in for loop compared to when it isn't -
i have multidimensional array values. use 2 loops loop through each item , add value them. while in array return correct value, when exits , value different. example:
- before loop:
[[1,1],[2,2],[3,3]]
- during loop:
[[2,2],[3,3],[4,4]]
- after loop
[[50,50][65,65][90,90]]
it seems randomly change.
int[,] squareb = basesquare; int[,] squarec = basesquare; int[,] squared = basesquare; console.writeline("{0}", squareb[0, 1]); (int x = 0; x < sub; ++x) { (int y = 0; y < sub; ++y) { squareb[x, y] += 9; console.writeline("{0}", squareb[x, y]); squarec[x, y] += 18; squared[x, y] += 27; } } console.writeline("{0}", squareb[0, 1]);
you're modifying exact same array tree times @ every single loop iteration:
squareb[x, y] += 9; squarec[x, y] += 18; squared[x, y] += 27;
these change exact same value.
Comments
Post a Comment