Here " Key " is any unique token which is used to identify its corresponding data or value uniquely, "Value" is the data which you want to store it can be any thing string, integer, float etc..
Both(Idictionary and Hashtable) are derived from System.Collections Class. are used for
1) Inserting Data
2) Deleting data
3) Modifying Data
4) Searching for a particluar value
Idictionary:
In Idictionary we need to clearly define what type of value we want to store. let say we want to store both key and value as strings, here is the syntax
IDictionary < string,string > Name;
Name= new Dictionary < string,string >
So in the above we have created Name as Idictionary where Key and value both takes strings.
Hashtable:
In hashtable no need to define any signature i.e you can store any kind of values. Hashtable take every key and value as an object but not as the value you have entered means eventhough we have entered Key and value as strings Hashtable internally treats them as objects but not as strings.
In the same hashtable you can store different values let say we have store Ist entry as (string, int) 2nd entry can be any thing let say (int, float). Since Hashtable treat every key or Value as an object .
Hashtable namelist;
nameList= new Hashtable();
Differences between Hashtable and Idictionary:
- In Idictionary we need to explicitly define signature i.e what type of Key and value we want to store. Hashtable no need to define any structure or signature.
- In Idictionary once we have defined signature there is no chance of entering values other than the signature we have specified. where as within the same hashtable we can enter any type of key,value pair.
- In Hashtable we required Boxing and Unboxing to do any kind of manipulation with values. where as for Idictionary we don't require any kind of boxing and unboxing as we are already defined the signature.
- Hashtable is useful if you don't know what all kind of values you might be storing in it. Idictionary is used if you know exactly what kind of (key , value) pair you will be using.Let say if you know that both Key and value are always strings then use Idictionary.
- Hashtable is costly as every operation(inserting, modifying, deleting..) requires either Boxing or UnBoxing but where as Idictionary doesn't require any such things since we know the signature before hand.
