Introduction:
Here I will explain difference between arraylist and list in c#, vb.net with example or arraylist vs generic list in asp.net using c#, vb.net with example or difference between generic list and arraylist collection in c#, vb.net with example. In c# or vb.net we use arraylist and generic lists to store any type of data in a collection format.
Description:
In
previous posts I explained difference between char varchar nvarchar in sql, difference between website and web application in asp.net, difference between bit tinyint smallint int and bigint
datatypes in sql server, difference between appsettings and connectionstrings in
web.config, difference between ref and out in c#, vb.net with example
and many articles related to interview questions, asp.net, c#.net. Now I will explain
difference between arraylist and generic list in c#, vb.net with example.
ArrayList
Arraylist is used to
store any type of data there is no restriction that mean we can store integer
or string or any object based on our requirements. If we want to use ArrayList
in our applications we need to add System.Collections
namespace and ArrayList was introduced in .NET 2.0 framework.
C# Code
using System.Collections;
ArrayList arylist = new
ArrayList();
arylist.Add("Suresh
Dasari");
arylist.Add("aspdotnet-suresh.com");
arylist.Add(2046);
arylist.Add(DateTime.Now);
|
VB.NET Code
Imports System.Collections
Dim arylist As New ArrayList()
arylist.Add("Suresh
Dasari")
arylist.Add("aspdotnet-suresh.com")
arylist.Add(2046)
arylist.Add(DateTime.Now)
|
If we want to get
data from ArrayList we need to loop through that list as an object data type. In ArrayList the
disadvantage is we don’t know what type of elements it will return while
looping through an array object because we can store any type of elements.
Suppose if we want to
get particular element from ArrayList we must be aware that what type of
element that is otherwise it will throw error in case if we are assigning string element to integer datatype element.
C# Code
foreach(object obj in arylist){
}
|
VB.NET Code
For Each obj As Object In arylist
Next
|
Generic List (List<T>)
Generic lists are the
type safe objects because it will store only specified datatype of elements.
The Generic List declaration will be like List<T>
here T means datatype i.e. either
string, integer, etc.
If we want to use Generic
List in our applications we need to add System.Collections.Generic
namespace and Generic List was introduced in .NET 3.0 framework.
C# Code
using System.Collections.Generic;
List<int>
lst = new List<int>();
lst.Add(232);
lst.Add(534);
lst.Add(2046);
|
VB.NET Code
Imports System.Collections.Generic
Dim lst As New List(Of Integer)()
lst.Add(232)
lst.Add(534)
lst.Add(2046)
|
If we want to get
data from Generic List we can loop through that list with same object datatype whatever
we defined while declaring Generic List. So to access elements from Generic
List we won’t get any problem because we already aware that what type of
elements our List contains.
C# Code
foreach (int num in lst)
{
}
|
VB.NET Code
For Each num As Integer In lst
Next
|
I hope
it helps you to understand difference between arraylist and generic list.
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. |
|||
|
|||
7 comments :
Nice description
Thanks bro...................
nicely explained
cool man.
Good explanation. very helpful for beginners.
Good explanation. very helpful for beginners.
good explanation
Really good Suresh.i am creak any type of Asp.net interview helping to your Blog..
Cheers..
Note: Only a member of this blog may post a comment.