Sunday, 30 July 2017

Difference Between Array And ArrayList In C#

Introduction
In this article we will learn what is the difference between Array and ArrayList. What are arrays and why do we need array. What are arraylist and why do we need arraylist. how to use arraylist in c#.

Previous Updates

In previous articles we have learnt  Why every business needed digital Marketing  ,What is Lock and how to achieve lock on sql tableAuthorization in Asp.Net. How high quality content affects your Website. What is Blocking and Deadlock In SQL. Transaction Commit and Rollback in sql server with example. Top 30 Asp.net interview question

Arrays
Arrays are strongly typed collection of same type of datatype. Arrays are fixed in length , you can not change array length at runtime. Arrays are stores values in indexes and if we need to access the values from array then we have to read the index of array. It is a refrence type datatype.

Declaration and assigning values to Array
string[] strArr=new string[2];
strArr [0] = "Web Development";
strArr [1] = "GurujiPoint";
In this code we declare string type of array having size 2. And assigning values to strArr on 0 and 1 index.


ArrayList
Arraylist are not strongly type collection. Arraylist can store values of same type as well as different data types. It is not fixed in length you can change Arraylist size dynamically. Arraylist is usedSystem.Collection namespace.

Declaration and assigning values to ArrayList
ArrayList objArrList = new ArrayList();
objArrList.Add("welcome"); // Add string values
objArrList.Add(10);   // Add integer values
objArrList.Add(10.05); // Add float values

Difference between Array and ArrayList
Array
ArrayList
Arrays are strong type collection and allow to store fixed length
It is not strong type collection. List size increase or decrease during runtime.
It belongs to System.Array namespace
It belongs to System.Collection namespace
It stores only same type of data
It stores Different types of data.
Use static helper class Array to perform different tasks on the array.
ArrayList itself includes various utility methods for various tasks.

No comments:

Post a Comment