using System.Collections; // for Collections
using System.Collections.Generic; // for Generics
Collection classes are specialized classes for data storage and retrieval.
Collections are enhancement to the arrays.
Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc.
There are two distinct collection types in C#.
The standard collections, which are found under the System.Collections namespace (Array list, Hashtable, BitArray, Queue, SortedList, Stack)
And the generic collections, under System.Collections.Generic. (List, LinkedList, Dictionary)
The generic collections are more flexible and are the preferred way to work with data.
The generic collections or generics were introduced in .NET framework 2.0.
Generics enhance code reuse, type safety, and performance.
ArrayList
- It represents ordered collection of an object that can be indexed individually.
- It is basically an alternative to an array.
- However unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically.
- It also allows dynamic memory allocation, add, search and sort items in the list.
- Unlike arrays, an ArrayList can hold data of multiple data types.
ArrayList al = new ArrayList();
al.Add(45);
al.Add("hello");
al.Count;
al.Sort();
- Add( object value ); // Adds an object to the end of the ArrayList.
- AddRange( ICollection c ); // Adds the elements of an ICollection to the end of the ArrayList.
- Clear(); // Removes all elements from the ArrayList.
- Contains( object item ); // Determines whether an element is in the ArrayList.
- GetRange( int index, int count ); // Returns an ArrayList which represents a subset of the elements in the source ArrayList.
- IndexOf(object); // Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
- Insert( int index, object value ); // Inserts an element into the ArrayList at the specified index.
- InsertRange( int index, ICollection c );// Inserts the elements of a collection into the ArrayList at the specified index.
- Remove( object obj ); // Removes the first occurrence of a specific object from the ArrayList.
- RemoveAt( int index ); // Removes the element at the specified index of the ArrayList.
- RemoveRange( int index, int count ); // Removes a range of elements from the ArrayList.
- Reverse(); // Reverses the order of the elements in the ArrayList.
- SetRange( int index, ICollection c ); // Copies the elements of a collection over a range of elements in the ArrayList.
- Sort(); // Sorts the elements in the ArrayList.
- TrimToSize(); // Sets the capacity to the actual number of elements in the ArrayList.
Hashtable
- The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key.
- It uses a key to access the elements in the collection.
- A hash table is used when you need to access elements by using key, and you can identify a useful key value.
- Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
Hashtable ht = new Hashtable();
ht.Add("001", "Zara Ali");
- Add( object key, object value ); // Adds an element with the specified key and value into the Hashtable.
- Clear(); // Removes all elements from the Hashtable.
- ContainsKey( object key ); // Determines whether the Hashtable contains a specific key.
- ContainsValue( object value ); // Determines whether the Hashtable contains a specific value.
- Remove( object key ); // Removes the element with the specified key from the Hashtable.
SortedList
- It uses a key as well as an index to access the items in a list.
- A sorted list is a combination of an array and a hash table.
- It contains a list of items that can be accessed using a key or an index.
- If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable.
- The collection of items is always sorted by the key value.
SortedList sl = new SortedList();
sl.Add("001", "Zara Ali");
- public virtual void Add( object key, object value ); // Adds an element with the specified key and value into the SortedList.
- Clear(); // Removes all elements from the SortedList.
- ContainsKey( object key ); // Determines whether the SortedList contains a specific key.
- ContainsValue( object value ); // Determines whether the SortedList contains a specific value.
- GetByIndex( int index ); // Gets the value at the specified index of the SortedList.
- GetKey( int index ); // Gets the key at the specified index of the SortedList.
- IList GetKeyList(); // Gets the keys in the SortedList.
- IList GetValueList(); // Gets the values in the SortedList.
- IndexOfKey( object key ); // Returns the zero-based index of the specified key in the SortedList.
- IndexOfValue( object value ); // Returns the zero-based index of the first occurrence of the specified value in the SortedList.
- Remove( object key ); // Removes the element with the specified key from the SortedList.
- RemoveAt( int index ); // Removes the element at the specified index of SortedList.
- TrimToSize(); // Sets the capacity to the actual number of elements in the SortedList.
Stack
It represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items.
When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.
Stack st = new Stack();
st.Push('A');
st.Push('H');
st.Peek() - H
st.Pop();
- Clear(); // Removes all elements from the Stack.
- Contains( object obj ); // Determines whether an element is in the Stack.
- Peek(); // Returns the object at the top of the Stack without removing it.
- Pop(); // Removes and returns the object at the top of the Stack.
- Push( object obj ); // Inserts an object at the top of the Stack.
- ToArray(); // Copies the Stack to a new array.
Queue
It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items.
When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.
Queue q = new Queue();
q.Enqueue('A');
q.Enqueue('M');
char ch = (char)q.Dequeue(); - Removes 'A'
- Clear(); // Removes all elements from the Queue.
- Contains( object obj ); // Determines whether an element is in the Queue.
- Dequeue(); // Removes and returns the object at the beginning of the Queue.
- Enqueue( object obj ); // Adds an object to the end of the Queue.
- ToArray(); // Copies the Queue to a new array.
- TrimToSize(); // Sets the capacity to the actual number of elements in the Queue.
BitArray
It represents an array of the binary representation using the values 1 and 0.
It is used when you need to store the bits but do not know the number of bits in advance.
You can access items from the BitArray collection by using an integer index, which starts from zero.
System.Collections.Generic;
List
A List is a strongly typed list of objects that can be accessed by index.
This is an efficient, dynamically-allocated array. It does not provide fast lookup in the general case (the Dictionary is better for lookups). List is often used in loops.
List<int> list = new List<int>();
list.Add(2);
list.Add(6);
list.Remove(2);
list.Insert(1,5);
int[] arr = new int[2];
arr[0] = 2;
arr[1] = 3;
list = new List<int>(arr);
Dictionary
- This is an implementation of a hash table: an extremely efficient way to store keys for lookup.
- Dictionary is fast, well-designed and reliable.
- A dictionary, also called an associative array, is a collection of unique keys and a collection of values, where each key is associated with one value.
- Retrieving and adding values is very fast. Dictionaries take more memory, because for each value there is also a key.
Dictionary<string, string> domains = new Dictionary<string, string>();
domains.Add("de", "Germany");
domains.Add("sk", "Slovakia");
foreach(KeyValuePair<string, string> kvp in domains) { }