-2

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:

  1. 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)
  2. The array for loop is not accruing any boxing and unboxing, So why would we need List?
  3. Which for loop (Array or List) is best in this scenario ?
  4. Which for loop will give good performance (I think both of for 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) ?
Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • 3
    No problem. I understand you want to be polite but try to avoid adding so much information which doesn't have much to do with the question next time - when it becomes cluttered it makes people more likely to close because they can't understand it - even if it's a good question! – Mansfield Oct 22 '13 at 12:15
  • They have not asking my scenario , mhm , Any way thanks !!! – Ramesh Rajendran Oct 22 '13 at 12:20
  • 2
    They **do** have your scenario. You can apply all points explained in that question to any scenario. Your scenario is answered by this line in the accepted answer: _"If you **know** the data is fixed length, **and you want to micro-optimise for some very specific reason** (after benchmarking), then an array may be useful"_. The point is - you're going to fill your array or list after a _database call_, which will take tens if not hundreds of milliseconds to complete. It really doesn't matter anymore if after that you take one microsecond to initialize your array, or two for your list. – CodeCaster Oct 22 '13 at 12:22
  • 1
    With regards to "boxing and unboxing", that doesn't occur because unlike java, C# can have generics on value types. – weston Oct 22 '13 at 12:27
  • @weston , It's a duplicate post , So please enough sir !! Thanks to all – Ramesh Rajendran Oct 22 '13 at 12:30
  • 1
    That part wasn't covered in the duplicate, so I thought I'd answer that at least. – weston Oct 22 '13 at 12:32
  • Okay , You know that , Hmm ! But Now it's totally it's a duplicated post . I asked 4 question , But i can't saw the good answers in my all question in that thread . hmm !! any way all is well !!! – Ramesh Rajendran Oct 22 '13 at 12:35
  • @CodeCaster . `If you know the data is fixed length, and you want to micro-optimise for some very specific reason (after benchmarking), then an array may be useful`. This answer is there , I know . But my question is `why` ?`what is the reason?` – Ramesh Rajendran Oct 22 '13 at 12:37
  • See for example http://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists. – CodeCaster Oct 22 '13 at 12:38
  • I'm sure I could find duplicates for the others if you like? :D – weston Oct 22 '13 at 12:50
  • Okay , do it quickly !! – Ramesh Rajendran Oct 22 '13 at 12:52

1 Answers1

2

When you create an array you specify the size. When you create your list you do not (you can provide an initial capacity with an overload of the constructor).

So when you create the array you say it needs memory for the size of 20 int objects. But when you create the List it has no size reserved for the ints.

List<int> list = new List<int>();
Console.WriteLine(list.Capacity);
//Output: 0

Then when you use the loop to add items to the array the size of the array remains 20. But when you use the loop for the list the list sees there is no room. So it creates room for 4 items (check list.Capacity after first add). Then when the capacity is full the list will double its capacity. Now it is 8. Then again 16, then again 32. So in the end your list reserved room in memory for 32 int objects.

The array only reserved 20 so in total the array is better memory wise.

Further more, performance wise, array is also a little faster. I refer to this SO answer: https://stackoverflow.com/a/454923/637425


To adress some of your questions:

  1. Many people don't like to work with indexes. You can make mistakes very easily (especially for starting programmers). Indexes are zero based etc etc... But if you could just call .Add() you don't have to worry about index. So it is a bit of convenience for most people to use List
  2. You don't need list... See answer 1.
  3. See my large story... Array is a bit better memory wise.
  4. Both give good performance and you wont notice any difference. When you start using larger lists/arrays then it becomes interesting. For something with the size of 20 there is just not much performance gain.
Community
  • 1
  • 1
SynerCoder
  • 12,493
  • 4
  • 47
  • 78