I am not asking about the difference between array and list in general, I'm just asking about the difference between them in this scenario.
I have one Array. The array index is a dynamic value, and I have one integer List.
int DynamicValue=20 ;//It's any values should be coming from database or anywhere
int[DynamicValue] array1=new int[DynamicValue];//Array declaration
List<int> list1=New List<int>();//List declaration
Now I create one for
loop to add values to Array and List:
//First add values in array
for(i=0;i<DynamicValue;i++)
{
array1[i]=i;
}
//Then add the values to list
for(i=0;i<DynamicValue;i++)
{
list1.Add(i);
}
Now what is this difference between the above codes ?
My questions are:
- Array is type safe and also defined the index size. So why do many peoples like Lists in this scenario? (I know it's useful for generics coding, but I'm asking for this scenario)
- The array
for
loop is not accruing any boxing and unboxing, So why would we need List? - Which
for
loop (Array or List) is best in this scenario ? - Which
for
loop will give good performance (I think both offor
loops are good, because this code does not attempt any boxing and unboxing and also this codes is type safe. But I'm not sure at this time which one is better) ?