Introduction:
Here I will explain difference between array and arraylist in c#.net with example.
Description:
In
previous posts I explained difference between wcf and web
application,
interview questions in asp.net, sql
server, c#,
Difference between functions and stored
procedures in SQL Server, difference between len and datalength
in sql server
and many articles relating to interview questions. Now I will explain difference between
array and arraylist in c#.net
with example.
Arrays
Arrays are strongly
typed collection of same datatype and these arrays are fixed length that cannot
be changed during runtime. Generally in 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
Generally
we will declare arrays with fixed length and store values like as shown below
string[] arr=new string[2];
arr[0] = "welcome";
arr[1] = "Aspdotnet-suresh";
|
In
above code I declared array size 2 that means we can store only 2 string values
in array.
Arraylists
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
ArrayList strarr = new
ArrayList();
strarr.Add("welcome");
// Add string values
strarr.Add(10); // Add integer
values
strarr.Add(10.05); //
Add float values
|
If
you observe above code I haven’t mentioned any size in array list we can add
all the required data there is no size limit and we will use add method to bind
values to array list
Difference between Array and ArrayList
Arrays
|
ArrayLists
|
These
are strong type collection and allow to store fixed length
|
Array
Lists are not strong type collection and size will increase or decrease
dynamically
|
In
arrays we can store only one datatype either int, string, char etc…
|
In
arraylist we can store all the datatype values
|
Arrays
belong to System.Array namespace
|
Arraylist
belongs to System.Collection namespaces
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
33 comments :
Very Useful !
Any noticeable performance difference? Obviously array is going to be faster but is it to a point that you should avoid ArrayList as often as you can?
thnx dude... these are the fadduuu definations...
Simple and very clear article. Thank you very much anna.
understanding very easily,great job
Gud Article brother thx
nice sanpshot.....for increasing the kownlaged about array & array list ......
Hi Suresh,
Very very ....useful to me :) keep rocking ......
very super brother great work................................thanks
sdgsde
Nice article.......
Very good info bro with simple example now i got the knowledge about arraylist thanks bro for posting this use full info
nice article suresh.can u explain the jagged array?
Nice Article can you explain the jagged Array concept?
Very NIce
In difference section,
In arrays we can store only one datatype either int, string, char etc…
In arraylist we can store all the datatype values
This is wrong difference. We can create array of any datatype.
e.g.
Home[] arr = { new Home(),new Home(),new Home()};
why can't store the different types in array collection with object type.
object[] arr= new object[3]
with tis we can store different data types
Thank u...explained simply and meticulously.
very usefull
@Suresh,
Very nice article... keep it posting ........
Owsm.........Simple and clear.......
Arrays:
string[] arr=new string[2];
arr[0] = "Hello";
arr[1] = "Great job";
Arraylist:
ArrayList strarr = new ArrayList();
strarr.Add("welcome"); // Add string values
strarr.Add(10); // Add integer values
strarr.Add(10.05); // Add float values
how can i add model values to array while the declaration of array.
really usefull!!!!!
Good explained.
nice article
Thanx Bro..Can you tell me how to use arraylist data
Good Article
I was searching for conceptual difference and I found it. Thanks sir....
we can have array of objects.. then what is the difference between the both ?
object[] objarray=new object[3];
ArrayList objlist = new ArrayList();
how to use arraylist aa parameter?
Note: Only a member of this blog may post a comment.