Everything about Collections in .Net Framework
Introduction
As a software developer you certainly knew something about collections
as an important data type in modern programming languages especially in the .Net
Framework 2.0 and above. As you may notice in the .Net Framework 2.0 Microsoft
dedicated more efforts to make collection classes reach, intuitive, have good
performance, and easy to use as possible. In our tutorial we will talk about
Collections in .Net, what they are, how they work, when to use them, and some
performance issues.
About Collections
We can define a collection as a set or group of related
items or records that can be referred to as a unit. A collection is a kind of
data structure; that is a way of storing data in a computer to be manipulated and
used in the most efficient way. starts from the base types data structures like
primitive types (characters, integers,...), passing by the composite types like
(unions), we can finally reach the linear types data structures like (arrays,
linked lists, stacks, queues, hash tables, ...) which are the subject of our
tutorial.
Generic and Non-Generic Collections
In the .Net Framework 1.x most of the collection classes
store a data of type "System.Object", so you can store any types of data in the
collection object or many types in the same collection object. This makes the
addition of new items very easy, but when you need to extract a stored data from
the collection you will first need to cast it back to its original data type.
This cast had a performance impact especially when you are going to deal with
the stored data repeatedly. It slows down your code plus making it less
readable.
Dim
List As New
System.Collections.ArrayList
List.Add("ABab")
List.Add(123)
List.Add(10.3)
In the .Net Framework 2.0 the notion of generic is
introduced to avoid the problem of casting as a source of performance drain and
to allow the compiler to catch any try to store a different type of data in the
specified collection at design time.
Dim
List As New
System.Collections.Generic.List(Of
String)
List.Add("Aa")
List.Add("Bb")
List.Add("Cc")
It is recommended to use generic collections. The only
reason you may need to use non-generic collections is for legacy purposes or if
you need your code to work with older versions of the .Net Framework. If you
need to store different types of data in a collection you can still use generic
collections, all what you need is to find a common base between the stored data
types. In the worst case you can use the System.Object as the generic type
parameter.
Dim
List As New
System.Collections.Generic.List(Of
System.Object)
Types of Collections in .NET
There are many types of collections in the .Net Framework
2.0. The most general types of them are under the "System.Collections" namespace.
All of them have the following basic methods and properties to manipulate the
stored data. "Add" to add a new element, "Remove" to remove an existing element,
and "Count" to get the total number of elements stored in a collection.
Non-Generic Collections
Found under the "System.Collections" namespace.
ArrayList: an array of contiguous indexed elements
whose size can be increased dynamically at run time as required. You can use this data
structure to store any types of data like integers, strings, structures,
objects, .....
BitArray: an array of contiguous bit values (zeros
and ones). Its size must be determined at design time.
SortedList: represents a list of key/value pair
elements, sorted by keys and can be accessed by key and by index.
Stack: represents the data structure of
last-in-first-out (LIFO).
Queue: represents the data structure of
first-in-first-out (FIFO).
HashTable: represents a collection of key/value
pair elements stored based on the hash code of the key.
Generic Collections
Found under the "System.Collections.Generic" namespace.
LinkedList: represents a doubly linked list data
structure of a specified data type.
Dictionary: represents a collection of keys and
values.
List: the generic counterpart of the ArrayList
collection.
Queue: the generic counterpart of the non-generic
Queue collection.
Stack: the generic counterpart of the non-generic
Stack collection.
When to Decide to Use a Collection?
Sometimes the answer is obvious, sometimes it is not. A
collection is used to tie certain items that have a similar nature together in
one set and refer to them as one group of data. As an example, when you need to
store some data about your customers, you can obviously use a collection for
this purpose. As a guideline you should use a collection when you have more than
one item that serve similar purposes and all have the same importance, when the
number of items are unknown at design time, when you need to sort items, when
you need to iterate through items, or when you need to expose the items to a
consumer that expects collection type.
Some performance issues when woring with collectins
The main purpose of collections is to deal with a large
number of items, and as the number of items increased the performance of your
application will be affected. To reduce the effect of the number of items on
your application you have to choose the right kind of collection you will use.
This depends on the usage patterns that are expected from your collection. You
have to decide whether you will insert all the items in the collection only on
initialization of your application, or you will do that through out the life
time of the application. How many times will you need to iterate through the
items of the collection. Will you need to insert new items at the middle, or
just add the new ones to the end of the collection. Is it important to store
your items in order or not, and so on. You have to decide what is the key
operation you need; is it iteration?, insertion, lookup, or saving data in
order. You can choose the right collection depending on the usage pattern of
data.
For example when your main concern is lookups and you
don't mind about the order of items, your first choice should be
System.Collections.Generic.Dictionary. that has a very fast lookups plus quick
additions and removals. The mentioned three operations operate quickly even if
the collection contains millions of items.
Dim
Dic As New
System.Collections.Generic.Dictionary_
(Of
Integer, String)
Dic.Add(12,
"Aa")
Dic.Add(20,
"Bb")
Dic.Add(100,
"Cc")
Dim
s As String =
Dic.Item(20)
Dic.Remove(100)
If your usage is mostly addition and a few deletions, and
if it is important for the logic of your program to keep items in order you will
need to use the System.Collections.Generic.List collection. In this situation
lookup will be slow because the need to traverse the entire list items to get
the required one. Inserting items at the middle of the collection or removing
existing items will take a valuable amount of time too, but adding items at the
end will be very quick.
Dim
L As New
System.Collections.Generic.List(Of
String)
L.Add("Aa")
L.Add("Bb")
L.Add("Dd")
L.Insert(2,
"Cc")
L.Remove("Bb")
This tutorial is written by
ThinkAndCare.
|