What are Collections?

A collection object is an object that can hold multiple references of some other type of objects. Different collections in OO  

  • Array –  In C# arrays are objects.
  • Ordered lists  – similar to array. elements placed in a particular order and retrieved in same order
  • SortedList  – similar to ordered list. The elements are sorted when added to collection
  • Sets – unordered collection. (like throwing the marbles into bag. we dont know which will come out when we pick one.. order of retrival is not known). Important aspect of set is duplicates are not allowed.
  • Dictionaries (in C# HashTable) – provides a way for storing elements with a unique lookup key for the element.

User defined Collections :  You can create user defined collections using the inbuilt collection types.  Just like you use basic datatypes to create a user defined data type, you can use the inbuilt collections to build user defined collections.

Collections of SuperTypes :    Array is sequence of memory locations where elements of same type are stored.  This is true for an array of string, int or some other primitive datatypes.  But for storing the references of the objects (instances of classes), the rules are bit different.  The rules are different because  of the power of inheritance.   It turns out that if we create an array of baseclass (lets call it Class A), then the array can store the references of objects of base class (ie Class A) and all the derived class of the base class. 

In C# only arrays allow us to specify the type of class (ie type of array)  . All other collection types dosent  allow us to  specify the type. Most collections in C# are designed to hold the type System.Object (which is the base class for all the collections for both user defined and in-built datatypes) . So you can put any type of  objects into collections in any order.

Advertisements

Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

2 thoughts on “What are Collections?”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s