Search code examples
c#asp.net.net-2.0

ArrayList to int[] in c#


I have my declaration as follows

int[] EmpIDs = new int[] { };
ArrayList arrEmpID = new ArrayList();
int EmpID = 0;
if (CheckBox1.Checked)
{
   EmpID = 123;
   arrEmpID.Add(EmpID);
}
if (CheckBox2.Checked)
{
   EmpID = 1234;
   arrEmpID.Add(EmpID);
}
if (CheckBox3.Checked)
{
   EmpID = 1234;
   arrEmpID.Add(EmpID);
}

After all i would like to assign this the EmpIDs which was declared as int array

I tried this

for (int i = 0; i < arrEmpID.Count; i++)
    {
        EmpIDs = arrEmpID.ToArray(new int[i]);
    }

But i am unable to add can any one help me


Solution

  • You should avoid using ArrayList. Instead use List<int>, which has a method ToArray()

    List<int> list = new List<int>();
    list.Add(123);
    list.Add(456);
    int[] myArray = list.ToArray();