Array
Arrays are strongly typed collection of same datatype and these arrays are fixed length that can’t be changed during runtime. Arrays we will store values with index basis that will start with zero. If we want to access values from arrays we need to pass index values.
Declaration of Arrays
We can declare arrays as shown below.
- using System;
- namespace WebApplication2
- {
- public partial class _Default: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- string[] strNames = new string[]
- {
- "Ramakrishna",
- "Praveenkumar",
- "Indraneel",
- "Neelohith"
- };
-
- string[] strNamesObj = new string[4];
- strNamesObj[0] = "Ramakrishna";
- strNamesObj[1] = "Praveenkumar";
- strNamesObj[2] = "Indraneel";
- strNamesObj[3] = "Neelohith";
- }
- }
- }
Arraylist
Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type. These Array lists will be accessible with “System.Collections” namespace.
Declaration of Arraylist
To know how to declare and store values in array lists check below code.
- using System;
- using System.Collections;
- namespace WebApplication2
- {
- public partial class _Default: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ArrayList arrListObj = new ArrayList();
-
- arrListObj.Add("Ramakrishna Basagalla");
-
- arrListObj.Add(1981);
-
- arrListObj.Add(11.99);
- }
- }
- }
In the above example we have not specified any size to an array list, we can add any number of data there is no size limit.
Difference between Array and ArrayList
Array | ArrayList |
Arrays are strongly typed collection of same datatype. | ArrayList are not strongly typed collection we can store different types of data. |
Data is fixed length. | We can increase data dynamically. |
Arrays belongs to System.Array namespace. | ArrayList belongs to System.Collection namespace. |
Data can be added using array index. | Data can be added using Add method. |
Hope this blog will help you in understanding the Array and ArrayList. Happy Coding.