Many a times closely related objects can be handled effectively by creating their group. This is achieved in C# by using collections. There are two ways which we can use for grouping the objects together.
1. By creating array of objects
2. By creating collection of objects.
Arrays are most useful for creating and working with a fixed number of elements of the same data type
Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections such HashTable or Dictionary, you can assign a key to any object that you add to the collection so that you can retrieve the object by using the key.
Types of Collections
NET Framework Class library provides collection classes that to work with a set of objects. Their classification is listed below
The following groups of collection classes are described in this section:
- System.Collections classes
- System.Collections.Generic classes
- Visual Basic Collection class
- System.Collections.Concurrent classes
- System.Collections Classes
The classes in System.Collections namespace allows storing elements of any data type in a single collection. Every element is stored as an object of type Object. So these collection classes are typically referred as non generic classes.The following tabular lists some of the frequently used classes in the System.Collections namespace:
|ArrayList||Represents an array of objects whose size is dynamically increased as required.|
|Hashtable||Represents a collection of key/value pairs that are organized based on the hash code of the key.|
|Queue||Represents a first in, first out (FIFO) collection of objects.|
|Stack||Represents a last in, first out (LIFO) collection of objects.|
- System.Collections.Generic Classes
In addition to non generic classes .NET FCL provides a set of strongly typed collection classes which allows storing a set of element of the specific data type. They are available in the System.Collections.Generic namespace
.Net languages also support defining generic methods, delegates and interfaces . These built-in generic collection classes, methods, interfaces, structures which have a placeholder <T> for one or more types they store.
A generic collection is typically used to store the items in of the same data type. It enforces strong typing by allowing only the desired data type to be added. Datatype has to be specified at the time of instantiation of generic classes
The following tabular lists some of the frequently used classes of this namespace:
|Dictionary<TKey, TValue>||Represents a collection of key/value pairs that are organized based on the key.|
|List<T>||Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists.|
|Queue<T>||Represents a first in, first out (FIFO) collection of objects.|
|SortedList<TKey, TValue>||Represents a collection of key/value pairs that are sorted by key based on the associated IComparer<T> implementation.|
|Stack<T>||Represents a last in, first out (LIFO) collection of objects.|
Let us create an object of thegeneric Stack collection that stores the values of the type int only as shown in the following code snippet.
To set the data in generic stack, create the object and call its Push () method
To display the data from the stack, call Pop() method
The data placeholder variable does not allow to store the values on any other data type except int ; as the data type of the placeholder <T> is of type int. If the value of any other data type is pushed on to it, it will result in compile time error.
3. Visual Basic Collection Class
A Visual Basic Collection is an ordered set of items that can be referred to as a unit. The namespace required for this class is Microsoft.VisualBasic
You can use the Visual Basic Collection class to access a collection item by using either a numeric index or a String key in VB.NET. You can add items to a collection object either with or without specifying a key. If you add an item without a key, you must use its numeric index to access it.
The Visual Basic Collection class stores all its elements as type Object, so you can add an item of any data type. There is no safeguard against inappropriate data types being added.
When you use the Visual Basic Collection class, the first item in a collection has an index of 1. This differs from the .NET Framework collection classes, for which the starting index is 0.
You can create a collection the same way you create other objects. For Example,
The following code snippet creates an object of Collction and the adds items ot it using Add() method
Dim mNames As New Microsoft.VisualBasic.Collection()
The following For –Each iterator display the items in the names
For Each mName As String In mNames
Console.WriteLine (mName )
4. System.Collections.Concurrent Classes
The .NET Framework 4, includes the System.Collections.Concurrent namespace which includes collection that provide efficient thread-safe operations for accessing collection items concurrently from multiple threads.
Some classes included in the System.Collections.Concurrent namespace are
|BlockingCollection<T>||Provides blocking and bounding capabilities for thread-safe collections that implement IProducerConsumerCollection<T>.|
|ConcurrentBag<T>||Represents a thread-safe, unordered collection of objects.|
|ConcurrentDictionary<TKey, TValue>||Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.|
|ConcurrentQueue<T>||Represents a thread-safe first in-first out (FIFO) collection.|
|ConcurrentStack<T>||Represents a thread-safe last in-first out (LIFO) collection.|