O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

  • 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 ensure that key is unique when calling the Add method, you can call the ContainsKey method beforehand.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required