Difference Between Array List and Array in C#

 

Array

Arrays are strongly typed arrays. They usually start with zero indexed so that can call by using indexes during runtime. These arrays are of specified length that cannot be change during runtime.

Declaration:


To declare the array use "[]" brackets after data type and then assign the fixed length or fixed size to an array as shown below.

 
static void Main(string[] args)
{
int[] ar = new int[10];
}

Above code shows that we have declared an integer type array size of 10 (Means we can store up to 10 integer values in that array). You can repeat the same process for different data types as shown below.

 
static void Main(string[] args)
{
int[] ar = new int[3] {1,2,3};
string[] ar = new string[3] {“A”,”B”,”C”};
double[] ar = new double [3] {0.1,0.2,0.03};
}

Accessing an array:

To access or to fetch values from an array you can use loops. Best practices to use FOR or FOR-EACH loops.

 
static void Main(string[] args)
{
 int[] ar = new int[3] {1,2,3};
 foreach(int i in ar){
   Console.Writeline(i);//Displays 1, 2, 3
 }
}

Above code access all values from an array by using for-each loop and display them as output.

What is an Array List?

Array List is not strongly type. They are implemented from collection base that is "System.Collection".Array List resizes dynamically it can take any size of values from any data type. It stores a collection of values from different data types or same data types. If the values stored in collection are of different data types then type cast is must.


Declaration:

Declaration of an Array List is pretty simple first import namespace i.e. "System.Collection". Then call Array-List followed by Array-List object name assign it to Array-List.

 

 
Using System.Collection;
static void Main(string[] args)
{
ArrayList arrList = new ArrayList();
}

To add values to an array-list you can call "Add" method of Array-List to keep adding values continuously as shown below.

 
using System.Collection;
static void Main(string[] args)
{
ArrayList arrList = new ArrayList();
arrList.Add(“A”);//Added string value
arrList.Add(1);//Added integer value
arrList.Add(0.05);//Added real value
}

Accessing an array-list:

To access or to fetch values from an array-list you can use loops. Best practices to use FOR or FOR-EACH loops.

 
static void Main(string[] args)
{
  ArrayList arrList = new ArrayList();
  arrList.Add(“A”);//Added string value
  arrList.Add(“B”);//Added string value
  arrList.Add(“C”);//Added string value

  foreach(string stri in arrList){
     Console.Writeline(stri);//Displays A, B, C
  }
}

Above code access all values from an array-list by using for-each loop and display them as output.

Difference between Array-List and Array.

  • Arrays are strongly typed
  • Array-Lists are not strongly typed.
  • Elements in Arrays have to be of same data type (int, string, double, char, bool…).
  • Elements in Array-List can have a combination of combined data types or single data type. Note: If Array-List has combined data types then type cast is must.
  • Arrays are fixed specified length size therefore they cannot be resize dynamically during runtime.
  • Array-List can resize dynamically during runtime

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Apr
22
2013

How to Create and Initialize Jagged Arrays using C#

CLICK TO READ FULL ARTICLE

Views - 19925 |Category - C#

Apr
20
2013

Try, Catch and Finally Blocks in C#

CLICK TO READ FULL ARTICLE

Views - 17021 |Category - C#

Apr
19
2013

Abstract Class and Abstract Method With An Example Using C#

CLICK TO READ FULL ARTICLE

Views - 49051 |Category - C#

Apr
17
2013

OOPS Principle - Abstraction in C# with an example and explanation

CLICK TO READ FULL ARTICLE

Views - 154779 |Category - C#

Apr
16
2013

What are Delegates in C# - Step By Step Creating and Using the Delegate

CLICK TO READ FULL ARTICLE

Views - 18520 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

uip26

 

 
Comments

Posted by Gayatri on 2014-12-29

very well explained

Posted by Michael kamara on 2014-07-22

very helpful: simple enough.

Posted by Anonymous on 2014-04-17

Very nice article!!

Posted by Sahir on 2014-04-17

i had attend an interview question Can you write the code and give explain for this code The task is How to split and seperate the integer and strings in arraylist : Condition is string and variable should be Random that means string should be Stored in one method and integer should be stored in anothr method after that sort the strng and integer ArrayList a = new arrayList(); a.Add("sahir"); a.add(1); a.Add("Mohamed"); a.Add(5); a.Add(yr"); a.Add(67); if you can just help me sir

Posted by Nisha on 2014-04-11

Good and simple to understand

Posted by Vaseem on 2013-10-03

good diff.