I am trying to fix the size of the Hashtable with following code.
Hashtable hashtable = new Hashtable(2);
//Add elements in the Hashtable
hashtable.Add("A", "Vijendra");
hashtable.Add("B", "Singh");
hashtable.Add("C", "Shakya");
hashtable.Add("D", "Delhi");
hashtable.Add("E", "Singh");
hashtable.Add("F", "Shakya");
hashtable.Add("G", "Delhi");
hashtable.Add("H", "Singh");
hashtable.Add("I", "Shakya");
hashtable.Add("J", "Delhi");
I have fix the size of this Hashtable is 2 but I can add more than 2 elements in this, why this happen, I am doing somthing wrong?
I have tried to find out is this Hashtable have fix size of not with hashtable.IsFixedSize, it always returns false
Please tell me where I am wrong, or there is another way..
Once you’ve added all the elements you need, you need to implement a read-only version of
IDictionarythat you can then use to disallow altering existing entries (look at subclassingHashtable, and overriding all the methods/properties which alter values to throwNotSupportedException). You can then overrideIsReadOnlyandIsFixedSizeto return whatever values you need.I would also recommend you use the generic
Dictionary<TKey, TValue>instead ofHashtable, as you get better compile-time safety and better performance with value types. However, you would have to create your own read-only implementation ofIDictionary<TKey, TValue>, as none of the Dictionary methods are virtual.