Name

Hashtable.Add Method

Class

System.Collections.Hashtable

Syntax

hashtablevariable.Add(Key, Value)
Key

Use: Required

Data Type: Object

The hash table entry’s key

Value

Use: Required

Data Type: Object

The hash table entry’s value

Return Value

None

Description

Adds a key/value pair to the hash table

Rules at a Glance

  • Key must 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.

  • Value need not be unique.

Programming Tips and Gotchas

  • According to the documentation, it is better to build a key from a String object than the Base Class Library’s StringBuilder object.

  • The Item property can also be used to add new members to the hash table.

  • To insure that key is unique when calling the Add method, you can call the ContainsKey method beforehand.

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.