0 votes
65 views
Difference between Arrays , List & ArrayList in c# programming
asked Apr 26, 2016 in Computer Science - IT by slow_learner | 65 views

2 Answers

+1 vote

An Array (System.Array) is fixed in size once it is allocated. You can't add items to it or remove items from it. Also, all the elements must be the same type. As a result, it is type safe, and is also the most efficient of the three, both in terms of memory and performance.

Use c# array when you are dealing with data that is:

  • fixed in size, or unlikely to grow much
  • suitably large (more than 10, 50, 100 elements, depending on the algorithm)
  • you will be doing lots of indexing into it, i.e. you know you will often want the third element, or the fifth, or whatever.

A List<> leverages generics; it is essentially a type-safe version of ArrayList. This means there is no boxing or unboxing (which improves performance) and if you attempt to add an item of the wrong type it'll generate a compile-time error.

Use a c# list for:

  • variable length data lists
  • that are mostly used as a stack or a queue or need to be iterated in its entirety
  • when you do not want to write an expression to derive the ultimate array size for the declaration and you do not want to wastefully pick a large number
answered Jan 15 by edwardcorner
0 votes

Simple difference between them are :

1. Arrays : are used to store elements with same datatype

They are best suited when you know number of elements are available 

String array example :

string[] studentName={"able","kumar","raj"};

2. List : are also used to store variables

Here we dont have to bother about number of elements , also we can easily add or delete elements from list

List<string> myCollection = new List<string>();

myCollection.Add(value_goes_here);

3. ArrayList : 

In arraylist you can accomodate multiple array values or group of arrays

 ArrayList lists = new ArrayList();
 lists.Add(items_goes_here);
answered Apr 26, 2016 by slow_learner