E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Generic Collections
by Ryan Olshan

You should use Generic collections in .NET 2.0 instead of non-Generic collections, due to the addition of overhead that is incurred when casting objects in non-Generic collections. Generic collections live in the namespace System.Collections.Generic.


Non-Generic and Generic Collection Equivelents

Non-GenericGeneric
ArrayListList<Value>
CollectionBaseCollection<Value>
DictionaryBaseN/A (implement IDictionary<Key, Value>
HashTableDictionary<Key, Value>
ICollectionN/A (use IEnumerable<Value> or anything that extends it)
IEnumerableIEnumerable<Value>
IListIList<Value>
QueueQueue<Value>
ReadOnlyCollectionBaseReadOnlyCollection<Value>
SortedListSortedList<Key, Value>
StackStack<Value>
N/AICollection<Value>
N/AKeyedCollection<Key, Item>
N/ALinkedList<Key, Value>
N/ASortedDictionary<Key, Value>


Non-Generic Collection Example: ArrayList

In .NET 1.x, a collection of values could be stored in a collection. The demo below is using an ArrayList.

VB.NET
Dim arrayNames As new ArrayList()
arrayNames.Add("Bob")
arrayNames.Add("Sally")
arrayNames.Add("John")


C#
ArrayList arrayNames = new ArrayList();
arrayNames.Add("Bob");
arrayNames.Add("Sally");
arrayNames.Add("John");


Values added to any collection are added as type object. In order to retrieve a value from the ArrayList, we access it by its index:

VB.NET
Dim strName As string
strName = arrayNames(1)


C#
string strName;
strName = (string)arrayNames[1];


Because the values are stored as objects they need to be type casted. In VB.NET, this is done automatically using late binding. In C#, you have to implicitly cast the type. Both operations add overhead.

Generic Collection Example: List

In .NET 2.0, a generic collection can be used. When the collection is instantiated, the datatype is declared. As a result, the values are strongly typed as the datatype of the list/values are known at runtime. In the example below, we use the Generic collection equivelent of an ArrayList.

VB.NET
Dim listNames As new List(Of String)
listNames.Add("Bob")
listNames.Add("Sally")
listNames.Add("John")


C#
List<string> listNames = new List<string>;
listNames.Add("Bob");
listNames.Add("Sally");
listNames.Add("John");


Because no type casting is involved, we can access a value in the List collection as follows:

VB.NET
Dim strName As string
strName = listNames(1)


C#
string strName;
strName = listNames[1];

There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.