Collections in C#

Collections in C#

This article discusses one of the most important parts of .Net Framework: Collections. In C#, a collection is a group of objects. Simply, Collection represents a set of objects that you can access by stepping through each element in turn. The .NET Framework provides specialized classes for managing collection and these classes have rich capability for enhancing your programming experience through better performance and easy maintenance.

The principal benefits of collections are that they standardize the way groups of objects are handled by your programs. All collections are designed around a set of cleanly defined interfaces.Several built-in implementations of these interfaces, such as Array List, Hashtables, Stack and Queue etc, are provided, which you can use as-is. You can also implement your own collection, but you will seldom need to.

The .Net framework contains a large number of interfaces and classes that define and implement various types of collection. Object class is the base class of every type in .NET. All the collections implement IEnumerable interface that is extended by ICollection interface. IDictionary and IList are also interfaces for collection which are derived from ICollection as shown in below diagram.

System.ICollection.IEnumerable:

It exposes the enumerator, which provides a collection, according to user defined classes.

Methods:

GetEnumerator (): It returns the enumerator object that can be used to iterate through the collection. It allows using the foreach statement. Enumerators only allow reading the data in the collection.

System.Collections.ICollection:

ICollection interface specifies a method for getting the size of collection, creating enumerators on a collection and managing synchronized access to all non-generic collections. It is a base interface for classes in the System.Collections namespace.

Briefly, The basic Differences between Generic Collection and Non-Generic Collection:

Non-Generic collections - These are the collections that can hold elements of different data types. It holds all elements as object type. So it includes overhead of type conversions.

Generic collections - These are the collections that can hold data of same type and we can decide what type of data that collections can hold.

Code Clarification: In the above lines of code, declaring dynamic array and added four string object such as Pike, Whesley, Carl, Hodge etc, Count is property of ICollection interface which returns number of items in collection.

IsSynchronized: It returns true if access to the ICollection is synchronized.

SyncRoot: It returns an object that can be used to synchronize access to the ICollection.

Code Clarification: In above code, declare lock statement to hold that arryList object which is going to synchronize.

Important Methods:

CopyTo(): This method helps to copies the elements of the ICollection object to any array, starting at a particular Array index. If .NET is unable to cast source type to destination, then it throws ArrayTypeMismatchException exception.

System.Collections.IList:

IList interface represents the collection of objects that can be individually accessed by index. The implementation of IList has been divided into three categories: read-only, fixed-size, and variable-size. A read only IList cannot be modified. A fixed size IList does not allow the addition or removal of elements, but it allows the modification of the existing elements. A variables size IList allows the addition, removal, and modification of elements.

Important Properties:

IsFixedSize: It returns true if IList has fixed size.

ArrayList srcList = new ArrayList();

Console.Write(srcList.IsFixedSize); // returns- false because ArrayList is not fixed size

arryList.Add("Hodge"); // adding four string object and so on you can add many more

Insert ():

Insert (int index, object value):

It inserts an item to the IList at specific index. If index equals the number of items in the IList, then value is appended to the end, but if index greater than the number of items in the IList or less than zero, then it throws ArgumentOutOfRangeException exception. If you try to insert item in the read-only or fixed size IList then it throws NotSupportedException exception.

Contains ():

Contain (object value): It returns true if IList contain a specific value. This method uses the Equals and CompareTo methods to determine whether an item exists in Collection.

System.Collections.IDictionary:

It represents a collection of key/value pairs. IDictionary interface is implemented by classes that support collections of associated keys and values. Each element in a key/value pair is stored in a DictionaryEntry object. It allows the contained keys and values to be enumerated, but it does not imply any particular sort order.