Note: Collection are loosely coupled & Generics are strongly coupled.
Collections
Collections are of 4 types:
1. Index Based - Array, ArrayList, List
2. KeyVale Pair- HashTable, SortedList, Dictionary
3. Specialized - String Collection, CaseInsensitive Collection
4. Prioritised - Stack, Queue
Array
-----
3 things are not possible
1. Increased Size 2. Inserting in middle 3. Deleting or removing from middle
All these things are possible in Arraylist
ArrayList
---------
Intial default capacity is 0 (or to what we manually initialized)
Every Increment is by 4
HashTable
---------
While getting all the keys from the HT, it is not possible to get values in order since HT calculates the hash for every key(which is random), which is used in seraching algorithm. Hastable key is object based.
Searching in HT is incredible fats beacuse of hashes.
HashList is way fatser as comapred to array and arraylist
Generics
-------
Collection came in c# 1.0 . Generics came in C# 2.0
For every class in Collection namespace, there is an equivalent class in Colleciton.Generics namespace.
Collection<---> Generics
ArrayList <---> List
Hastable <---> Dictionary
IComparable Vs IComparer
------------------------
In my custom class, I can write a compare method if I want my class to perform sorting.
The method to implement is 'Compare(obj a)'.
If I have a class which is not my class, and I still want to sort the objects, I can use IComparer Interface.
IComparer has a method 'Compare(obj a, obj b)'.
Employee.sort()--->Implementing IComparable
Employee.sort(xyz)--> xmz is a class implementing IComparer.
IEnumerable
-----------
It is the parent interface for all collection classes.
Foreach in any class(List, Dictionary, ArraList, HT) works only because they have an implementation for foreach.
IEnumerable has a method 'GetEnumerator' -- actual eg from list class --- public Enumerator GetEnumerator();
This 'GetEnumerator' is the sole reason we can loop through all the entries in a colleciton.
Return type of 'GetEnumerator' is Eumerator class which implements from IEnumerator interface.
IEnumerator interface has 3 methods
IEnumerable
|--- IColleciton
|--- IList -- e.g. List, ArrayList
|--- IDictionary -- e.g. Dictonary, Hashtable
this means...
IList implements ICollection implements IEnumerable
IDicstionary implements ICollection implements IEnumerable
Side Notes:
C# 1.0 - Collections
C# 2.0 - Generics
C# 3.0 - var --- (checks/infers at compile time)
C# 4.0 - dynamic --- (checks/infers at run time)
Collections
Collections are of 4 types:
1. Index Based - Array, ArrayList, List
2. KeyVale Pair- HashTable, SortedList, Dictionary
3. Specialized - String Collection, CaseInsensitive Collection
4. Prioritised - Stack, Queue
Array
-----
3 things are not possible
1. Increased Size 2. Inserting in middle 3. Deleting or removing from middle
All these things are possible in Arraylist
ArrayList
---------
Intial default capacity is 0 (or to what we manually initialized)
Every Increment is by 4
HashTable
---------
While getting all the keys from the HT, it is not possible to get values in order since HT calculates the hash for every key(which is random), which is used in seraching algorithm. Hastable key is object based.
Searching in HT is incredible fats beacuse of hashes.
HashList is way fatser as comapred to array and arraylist
Generics
-------
Collection came in c# 1.0 . Generics came in C# 2.0
For every class in Collection namespace, there is an equivalent class in Colleciton.Generics namespace.
Collection<---> Generics
ArrayList <---> List
Hastable <---> Dictionary
IComparable Vs IComparer
------------------------
In my custom class, I can write a compare method if I want my class to perform sorting.
The method to implement is 'Compare(obj a)'.
If I have a class which is not my class, and I still want to sort the objects, I can use IComparer Interface.
IComparer has a method 'Compare(obj a, obj b)'.
Employee.sort()--->Implementing IComparable
Employee.sort(xyz)--> xmz is a class implementing IComparer.
IEnumerable
-----------
It is the parent interface for all collection classes.
Foreach in any class(List, Dictionary, ArraList, HT) works only because they have an implementation for foreach.
IEnumerable has a method 'GetEnumerator' -- actual eg from list class --- public Enumerator GetEnumerator();
This 'GetEnumerator' is the sole reason we can loop through all the entries in a colleciton.
Return type of 'GetEnumerator' is Eumerator class which implements from IEnumerator interface.
IEnumerator interface has 3 methods
IEnumerable
|--- IColleciton
|--- IList -- e.g. List, ArrayList
|--- IDictionary -- e.g. Dictonary, Hashtable
this means...
IList implements ICollection implements IEnumerable
IDicstionary implements ICollection implements IEnumerable
Side Notes:
C# 1.0 - Collections
C# 2.0 - Generics
C# 3.0 - var --- (checks/infers at compile time)
C# 4.0 - dynamic --- (checks/infers at run time)
No comments:
Post a Comment