Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Difference between ArrayList and List in C#.Net and VB.Net

Aug 10, 2016
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:

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.