I have simple class structure that has an array part of its fields. I want to store this class in a list. The problem is when adding additional class structures to the list it handles it as if it's a direct update and not referenced.
public class TrayLayout
{
public int[] inerD { get; set; }
public class TrayLayout
{
public int[] inerD { get; set; }
public TrayLayout(int[] inerD)
{
this.inerD = inerD;
}
}
public partial class Form1 : Form
{
public List<TrayLayout> trayL = new List<TrayLayout>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int[] aa = new int[2];
aa[0]=1;
aa[1]=2;
//add the new class to TrayLoayout
trayL.Add(new TrayLayout(aa));
aa[0]=3;
aa[1]=4;
//add the new class to TrayLoayout using input array
trayL.Add(new TrayLayout(aa));
aa[0]=5;
aa[1]=6;
//add the new class to TrayLoayout
trayL.Add(new TrayLayout(aa));
textBox1.Text = "the numbers accepted \n"+ trayL[0].inerD[0].ToString() + " , " +trayL[0].inerD[1].ToString() + " \n" + trayL[1].inerD[0].ToString() + " , " +trayL[1].inerD[1].ToString() + " \n" + trayL[2].inerD[0].ToString() + " , " +trayL[2].inerD[1].ToString() ;
}
I get in TextBoxes it shows the last input 5,6 5,6 5,6 instead of 1,2 3,4 5,6. I must be missing something?