Collection in c# | Type of collection

Collection in c# 

type of collection in c#

Collection provide a more flexible way to work with group of objects unlike array, the group of object you work can grow and shrink directly as the need of application change. 

For some collections, you can assign it to an object that you put into the collection so that you can quickly retrive the object by using the key.

Acollection is a class,you must declare a new collection before you can add element to that collection.

If your Collection contain elements of only on data type,you can use one of the classes in the System.Collection.Generics namespace.

A collection enforces type safety so that no other data type can be added to it. When you retrive an element from a generics collection collection,you do not have determine if data type for convert it.

Type of collection

Many common collection are provided by the .NET Fremework. Each type of collection is designed for a specific purpose. The following groups of collection classes are described in this section.

  • System.Collection.Generics
  • System.Collection

System.Collection class:

The classes in the System.Collection namespace do not store elements as specially typed objects,but as objects of type Object.

Whenever possible,you should use the generics collection in the System.Collection.Generics namespace or the System.Collection.Concurrent namespace instead of the legacy types in the System.Collection namespace.

Their are some frequently used classes in the System.Collection namespace.

#Queue:  Represents a first in, first out (FIFO) collection of object.
#Stack:    Represents a list in,first out (LIFO)  collection of object.
#ArrayList: Represents an array of objects whose size is dynamically increased as required.
#HasTable:  Represents a collection of key/value pairs the are organised based on the hash code of   the key.

System.Collection.Generic class:

You can create a generics collection by using on of the classes in the System.Collecion.Generic namespace. A generics collection is useful when every item in the collection had the same data type.
A generic collection enforces strong typing by allowing only the desired data type to be added.

Their are some frequently used classes in the System.Collection.Generic namespace.

#Dictionary<T Key, T Values>: Represents a collection of key/value pairs that are organized based on the key.
#List<T>: Represent a collection of object that can be accessed by index. Provides method  to search ,short and modify lists.
#Queue<T>: Represents a first in,first out (FIFO) collection of objects.
#Stack<T>: Represents a last in. first out (LIFO) collection of objects.


Generic allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in our program. In other word, generic allow you to write a class or method that can work with any data type.

You write the specification for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. 

Features of Generic

Generic is a technique that enriches your program in the following ways:

1. It helps you to maximize code reuse,type safety and performance.
2. You can create your own generic interfaces,classes,methods,events and delegate.
3. You may create generic classes constrained to enable access to method on particular data types.
4. You may get information on the types used in a generic data type at run-time by means of reflection.

Read More: