5 December 2013

Array, ArrayList, List and Dictionary in ASP.Net

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

Array
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:
    int[] intArray = new int[5];
    intArray[0] = 2;
    intArray[1] = 4;
    intArray[2] = 6;
    intArray[3] = 8;
    intArray[4] = 10;
    intArray[5] = 12; //Run time errorintArray[6] = "Hello";//Compile time errorforeach (int i in intArray)
    {
        Console.WriteLine("From array " + i);
    }
        Console.WriteLine("From array " + intArray[6]); //Run time error           

ArrayList
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:
     
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add(2);
    myArrayList.Add("OOPS");
    foreach (object obj in myArrayList)
    {
       Console.WriteLine("From arrayList " + obj.ToString());
    }
    Console.WriteLine("-------");

List
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:
     
    List<int> intList = new List<int>();
    intList.Add(2);
    intList.Add(8);
    intList.Add(4);
    intList.Add(6);
    foreach (int i in intList)
    {
       Console.WriteLine("From List " + i);
    }

Dictionary
Dictionary<keyDataTypevalueDataType> myCollection = new Dictionary<keyDataTypevalueDataType>();
  • 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:
     Dictionary<string,Employee> Employees =new Dictionary<string,Employee>();
    Employee emp1 = new Employee("Emp01","Employee1");
    Employee emp2 = new Employee("Emp02","Employee2");
    Employee emp3 = new Employee("Emp03","Employee3");
    Employee emp4 = new Employee("Emp04","Employee4");
    Employee emp5 = new Employee("Emp05","Employee5");
    Employees.Add(emp1.empID, emp1);
    Employees.Add(emp2.empID, emp2);
    Employees.Add(emp3.empID, emp3);
    Employees.Add(emp4.empID, emp4);
    Employees.Add(emp5.empID, emp5);
    Console.WriteLine(Employees["Emp05"].getNameAndID());
    foreach (KeyValuePair<string,Employee> emp in Employees)
    {
       Console.WriteLine(emp.Key +"  >>>>>  " + emp.Value.empName);
       Console.WriteLine(emp.Value.ToString());
    }

        public class Employee
        {
            public string empID;
            public readonly string empName;
            public Employee(string empID, string name)
            {
                this.empID = empID;
                this.empName = name;
            }

        }

No comments:

Post a Comment