• Difference between ArrayList and Generic List in ASP.NET

    by  • April 30, 2014 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET • 0 Comments

    ArrayList and Generic List are collections to store objects in it. Both are used for grouping of objects to perform different operations on it as per our requirements. Let’s see first about ArrayList and Generic List individually with code samples then we will see difference between these two.

    ArrayList

    ArrayList is an ordered collection of objects that can be indexed individually. It is actually an alternative of an array but in ArrayList, we can add and remove items at a specified position using an index.ArrayList are used to store any type of data as object. Each item is stored as an object so there is no need to specify any type to store data in ArrayList. We can store int, string and any other object in a single ArrayList and it can take any size of items from any data type. When we need to get these objects, we have to cast these object into proper type so there is no type safety and this may cause slow performance when ArrayList hold large number of objects.ArrayList automatically resizes itself when an item added or removed and it allows searching and sorting list items. ArrayList class in ASP.NET implements the IList interface. Let’s see how we can create and use ArrayList.

    You can create an ArrayList and add objects into it like below. As you can see, I have stored three values of different types in one object of ArrayList

    Now you can get values from ArrayList by type casting each value into its proper type.

    ArrayList will not cause any compile time error when we store different type objects in ArrayList but it will cause runtime errors.

    Generic List

    A Generic List is a strongly type collection of objects that can be accessed by index. We can search, sort and manipulate generic lists and we can add or remove at any specified position using index. Generic list contains data for a specific type which is mentioned when initializing list. As the data is stored in a specific type so no type casting is required when getting data from generic list. This ensures that there is no type mismatch at runtime. As we don’t need any type conversion so the performance is better in generic lists. In general, we can say that a generic list more efficient than ArrayList. The first thing we need to notice in generic list is the syntax which is like List<T>. Here <T> indicates the type we have to specify when declaring list. This enforces to specify a single type for which we need to save objects. It can be int, string or any custom created type. When we initialized the int generic list then we can only store int objects in that list and same is the case with string and other types. Let’s see how we can create and use generic list.

    You have to create generic list object for an individual type and you can store only that type of object into it.

    As you know we can store only one type of object in one generic list object so it will cause compile time errors when we want to add any object of different type in generic list.

    Difference between ArrayList and Generic List

    • Generic List is a strongly typed collection while ArrayList is not.
    • ArrayList is like any array of different object so we can store different type of objects in it while in generic list we can store only one type of object.
    • Each item in ArrayList is stored as an object while each item in generic list is stored as specific type.
    • We need to type cast when we have to get values from ArrayList while there is no type casting in generic list.
    • In ArrayList, we have to face boxing unboxing issues while in generic list, there are no boxing unboxing issues.
    • ArrayList belongs to System.Collections namespace while generic list belongs to System.Collections.Generic namespace.
    • ArrayList may not cause any compile-time errors but it will create runtime errors while on the other hand generic list will force compile-time errors so that there will be no runtime errors.
    • Generic list is better than ArrayList when we compare both on performance and efficiency.



    Namecheap.com
    Avatar of GetCodeSnippet.com

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply