Consider the following code:
internal class A
{
public int X;
}
private void test()
{
A[] Collection = new A[2];
Collection[0].X = 1;
Collection[1] = Collection[0]
Collection[0] = new A();
Collection[0].X = 2;
//The code above produces: Collection[1] displays 2, and Collection[0] displays 2.
//Wanted behaviour: Collection[1] should display 1, and Collection[0] display 2.
}
Since the array of classes, Collection, is a reference type. Collection[0] points to same memory region that Collection[1] does.
My question is, how can i "copy" Collection[0] values to Collection[1] so i get the following output:
Collection[1].X returns 1, and Collection[0].X returns 2.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…