To add a new value to a dotnet Hashtable I've always used:
myHashtable.Add(myNewKey, myNewValue);
but I've just come across some code which does the following instead:
myHashTable[myNewKey] = myNewValue;
Is there any difference between the two methods?
To correct Sergej's answer a little...
Add
will indeed throw an exception if the key already exists.IDictionary<TKey,TValue>
. In the non-generic IDictionary
implementations (e.g. Hashtable
) you'll get a null reference. You can't use a null key for either one though - you'll get an ArgumentNullException
.first will throw exception if there already were an item with given key and the second will throw an exception if there was no item with such key
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With