Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Using Generics in ASP.NET 2.0 using VB.NET

Author: Aadil

This article will show you what are Generics and how can they benefit you in using collection more efficiently and securely.

Generics are a new addition to the new .NET Framework. They are here to help you design and develop collections more securely and efficiently. In the previous releases of the .NET Framework you were given the System.Collections namespace that contained many classes for managing collection of objects. My most favorite ones were ArrayList and Hashtable. They allow you to store any thing in them and you can work with them by iterating over them of using the various functions provided in them for sorting, adding, removing and so on. In this new release, Microsoft has added a new namespace System.Collections.Generics that allows you to use a new type of collections. So why does a new kind of collections needed.


First of all you know when you are adding a new item in a collection that is based on System.Collections such as an ArrayList, if it is a Value Type object it needs to be boxed in to an object type. Boxing and Unboxing is a resourceful operation. Second is the measure for reliability. Lets say you are adding objects of type Person to an ArrayList and that ArrayList needs to be used by some other program that thinks that this ArrayList will contains only objects of Person. Now there is no simple way of restricting the ArrayList to add only objects of Person.


In a collection based on System.Collections.Generics you will always specify, before creation of the collection, the type of objects that this collection will contain. This will perform the reliability of the collection in that the program using this collection can be sure of that this is a collection of a specific type and there is no overhead of boxing and Unboxing using Generics.


Lets say you have to store objects of type Person in a List object in generic. The syntax will be


Dim lstPersons As New List(Of Person)


In this above statement we are declaring a List of type Person. The new keyword Of is an addition in the Visual Basic language. The new syntax of iterating over a collection in Visual Basic is


For Each p As Person In lstPersons




There are Generic collections of almost all types that were present in the SystemCollections namespace.

Article Comments
The article began with simple and clear language but It did not go detail of Generics. Is there any continuation ?

Posted on 11/15/2006 5:48:19 AM by Aparna

Add Article Comment:
Name :
Email Address :
Comments :
<< Socket Programming using Visual Basic.Net

Disclaimer - Privacy
© 2002-2017