I’ve written a simple class, point, and I want to use it in a list.
In the list I have 3 points. If I print it:
for (int i = listOfPoint.Count -1; i >=0; i--)
{
Console.WriteLine(listOfPoint[i].toString());
}
the output is:
(32, 10)
(33, 10)
(34, 10)
now, I want to do this:
for (int i = listOfPoint.Count -1; i > 0; i--)
{
listOfPoint[i] = listOfPoint[i - 1];
}
after this – print:
(33, 10)
(34, 10)
(34, 10)
after this – I want to change the Y value of listOfPoint[0], so:
listOfPoint[0].Y = 11;
print the list again and the output is:
(33, 10)
(34, 11)
(34, 11)
Why isn’t the output:
(33, 10) (34, 10) (34, 11)
And how can I do it like this?
This happen because you have two element of the array referencing the same object. This is due to pointers.
Try to do this to avoid the problem: