difference between arraylist and list collection
Problems with ArrayList
As we have seen in an earlier topic, ArrayLists are used to store any data which is inherited from System.Object. Which means, we can store an integer, string and custom object in a single ArrayList. To get the actual object, we need to cast them. This takes a performance hit when an ArrayList holds large number of objects.
Another disadvantage of ArrayLists is that there is no type safety. If we intended to create an ArrayList with only integers, and an attempt is made to add string data to the ArrayList, no compile time error is generated. ArrayList can hold different types of data, which will return an error at runtime. This is because we write code to work with one type of data. The solution to this problem is to restrict the type of data we can store in a collection and ensure that the collection contains only the required type of data.
Generics
The .Net framework provides the Generic type for collection classes and these classes ensure that a specific type of data can be stored in the collection. Now, we will discuss the generic type of ArrayList, i.e. List <T> class. Generics are the most powerful feature of C# 2.0. Generics allow us to define type-safe data structures. Generic collection types also generally perform better than the corresponding nongeneric collection types.
A generic collection is strongly typed (type safe), meaning that we can store only one type of data into the collection. This ensures there are no type mismatches at runtime. An additional benefit of generic collection is that performance is better because we do not need to converted them to actual object.
Suppose we want to store only integer values in a collection, we will specify the integer type for the generic parameter. The below code shows how to create a List. Note the data type between less than and greater than symbol <int>. This is how data type is declared at the time of instantiating the List object. In place of int we can use any type of object, like string or a custom business object.
As we have seen in an earlier topic, ArrayLists are used to store any data which is inherited from System.Object. Which means, we can store an integer, string and custom object in a single ArrayList. To get the actual object, we need to cast them. This takes a performance hit when an ArrayList holds large number of objects.
Another disadvantage of ArrayLists is that there is no type safety. If we intended to create an ArrayList with only integers, and an attempt is made to add string data to the ArrayList, no compile time error is generated. ArrayList can hold different types of data, which will return an error at runtime. This is because we write code to work with one type of data. The solution to this problem is to restrict the type of data we can store in a collection and ensure that the collection contains only the required type of data.
Generics
The .Net framework provides the Generic type for collection classes and these classes ensure that a specific type of data can be stored in the collection. Now, we will discuss the generic type of ArrayList, i.e. List <T> class. Generics are the most powerful feature of C# 2.0. Generics allow us to define type-safe data structures. Generic collection types also generally perform better than the corresponding nongeneric collection types.
A generic collection is strongly typed (type safe), meaning that we can store only one type of data into the collection. This ensures there are no type mismatches at runtime. An additional benefit of generic collection is that performance is better because we do not need to converted them to actual object.
Suppose we want to store only integer values in a collection, we will specify the integer type for the generic parameter. The below code shows how to create a List. Note the data type between less than and greater than symbol <int>. This is how data type is declared at the time of instantiating the List object. In place of int we can use any type of object, like string or a custom business object.
List<int> myInts = new List<int>();
ArrayList Articles
- retrieve arraylist items using for each loop
- retrieve arraylist items using indexers
- insert items to the arraylist using insert and insertrange method
- remove items from arraylist
- find item in arraylist using indexof method
- bind arraylist with gridview control in asp net application
- how to bind arraylist with dropdownlist in asp net application
- how to bind arraylist with radiobuttonlist in asp.net application
- convert arraylist to string array
- convert string array to arraylist
- convert datatable into an arraylist
- convert dataset to arraylist in asp.net using c#
- how to retrieve arraylist items in reverse order
- Arrays vs ArrayList
- how to create and add items into the ArrayList in csharp
- how to add items to the arraylist using an add method
- retrieve arraylist items using enumerators
Most Viewed
Home
Home
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls