Array, ArrayList, List and Dictionary in ASP.Net

Array, ArrayList, List and Dictionary all represent a collection of data but each is designed to satisfy different requirements. This article attempts to explain them with very basic examples; once you go through this article I hope the next time you will feel a little more comfortable deciding what to choose and why.

Array

  1. dataType[] myArray = new dataType[size];  
  • Fixed number of array size
  • Strictly type-safe
  • Collection of elements of the same data type only
  • Elements are accessed by zero-based index
  • Array class is in the System Namespace (in other words, it is automatically available)
  • Values are assigned by assignment operator as in the following:        
  1. int[] intArray = new int[5];  
  2. intArray[0] = 2;  
  3. intArray[1] = 4;  
  4. intArray[2] = 6;  
  5. intArray[3] = 8;  
  6. intArray[4] = 10;  
  7. intArray[5] = 12; //Run time error  
  8. intArray[6] = "Hello";//Compile time error  
  9. foreach (int i in intArray)  
  10. {  
  11.     Console.WriteLine("From array " + i);  
  12. }  
  13.     Console.WriteLine("From array " + intArray[6]); //Run time error
ArrayList
  1. ArrayList myArrayList = new ArrayList(); 
  • Size varies as required
  • Not type-safe
  • Collection of elements of the same or various data types
  • Reverse, Contains, Sort and a few other usefull methods are available that we don't get with Arrays
  • Useful when you are not sure about the size of the collection or you may need to have more than one data type in the collection
  • Elements are accessed by zero-based index
  • The data type of an ArrayList is an object type
  • Dictionary classes are in the System.Collections Namespace
  • Values are assigned by calling the Add method as in the following:
  1. ArrayList myArrayList = new ArrayList();  
  2. myArrayList.Add(2);  
  3. myArrayList.Add("OOPS");  
  4. foreach (object obj in myArrayList)  
  5. {  
  6.    Console.WriteLine("From arrayList " + obj.ToString());  
  7. }  
  8. Console.WriteLine("-------"); 
List
  1. List<T> myList = new List<T>(); // T for data type  
  • Size varies as required
  • Strictly type-safe
  • Collection of elements of the same data type only
  • Reverse, Contains, Sort and a few other useful methods are available that we don't get with Arrays
  • Useful if you want to have the features of both Array and ArrayList
  • Elements are accessed by zero-based index
  • Dictionary classes are in the System.Collections.Generic Namespace
  • Values are assigned by calling the Add method as in the following:
  1. List<int> intList = new List<int>();  
  2. intList.Add(2);  
  3. intList.Add(8);  
  4. intList.Add(4);  
  5. intList.Add(6);  
  6. foreach (int i in intList)  
  7. {  
  8.    Console.WriteLine("From List " + i);  
  9. }
Dictionary
  1. Dictionary<keyDataType, valueDataType> myCollection = new Dictionary<keyDataType, valueDataType>();
  • Size can vary
  • Can't have mixed data types, type-safe
  • The Collection is defined as a key-value pair
  • All of the keys must be of the same data type
  • All of the values must be of the same data type
  • Useful to deal with custom data type
  • Elements are accessed based on key
  • Dictionary classes are in the System.Collections.Generic Namespace
  • Key-Values are assigned by calling the Add method as in the following:
  1. Dictionary<string,Employee> Employees =new Dictionary<string,Employee>();  
  2. Employee emp1 = new Employee("Emp01","Employee1");  
  3. Employee emp2 = new Employee("Emp02","Employee2");  
  4. Employee emp3 = new Employee("Emp03","Employee3");  
  5. Employee emp4 = new Employee("Emp04","Employee4");  
  6. Employee emp5 = new Employee("Emp05","Employee5");  
  7. Employees.Add(emp1.empID, emp1);  
  8. Employees.Add(emp2.empID, emp2);  
  9. Employees.Add(emp3.empID, emp3);  
  10. Employees.Add(emp4.empID, emp4);  
  11. Employees.Add(emp5.empID, emp5);  
  12. Console.WriteLine(Employees["Emp05"].getNameAndID());  
  13. foreach (KeyValuePair<string,Employee> emp in Employees)  
  14. {  
  15.    Console.WriteLine(emp.Key +"  >>>>>  " + emp.Value.empName);  
  16.    Console.WriteLine(emp.Value.ToString());  
  17. }  
  18. public class Employee  
  19. {  
  20.     public string empID;  
  21.     public readonly string empName;  
  22.     public Employee(string empID, string name)  
  23.     {  
  24.         this.empID = empID;  
  25.         this.empName = name;  
  26.     }  
  27. }


Similar Articles