What are Collections?
May 29, 2006 by azagappan
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.