Array
Array is collection of homogeneous elements . It means we can only store the same type of data in our array . Let us see with the help of an example.
- int[] intList = new int[2];
- intList[0] = 1;
- intList[1] = 2;
In the above example, I created an array of integer type with its capacity. We can only store 2 integer type elements in the defined array because I have set capacity as two. Array works on the index based system, array index starts with position 0.
Remarks
In the array first you define which type of value you want to store and how many .You can’t store value as more than its capacity. Array size can’t automatically grow.
ArrayList
- ArrayList is available inside the System.Collections namespace.
- How to define the ArrayList.
- ArrayList arrayList = new ArrayList();
In the definition of ArrayList I did not use the capacity.
- How to add element to ArrayList.
We use the add method to add the element to the ArrayList as you can see from below code.
- arrayList.Add("shreesh");
- arrayList.Add(2) ;
- You can add object type data to the ArrayList as I added one string and another integer type.
- You can add as much value as you want . ArrayList size automatically grows according to new element.