Name
Hashtable.Add Method
Class
System.Collections.Hashtable
Syntax
hashtablevariable.Add(Key,Value)
-
Key(required; Object) The hash table entry’s key
-
Value(required; Object) The hash table entry’s value
Return Value
None
Description
Adds a key/value pair to the hash table
Rules at a Glance
Keymust be unique or a runtime error occurs.Keys are immutable. Once added, a particular key value cannot be changed during the lifetime of the hash table except by removing it through the Remove or Clear method and then adding it once again.
Valueneed not be unique.
Programming Tips and Gotchas
According to the documentation, it is better to build a key from a
Stringobject than the Base Class Library’sStringBuilderobject.The Item property can also be used to add new members to the hash table.
To ensure that key is unique when calling the Add method, you can call the ContainsKey method beforehand.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access