ProgrammingBackend Developer (VB.NET)

How is a dictionary with arbitrary keys (Dictionary) created and used in Visual Basic, what is the difference between Dictionary and Hashtable, and what are the features of working with missing keys?

Pass interviews with Hintsage AI assistant

Answer.

Background of the Issue:

In VB.NET, the Dictionary(Of TKey, TValue) class has become a prominent standard for associative data storage (key-value). In classic VB6, the Scripting.Dictionary object or the Collection collection was more commonly used, as well as the Hashtable type, which appeared in early versions of .NET. These structures allow for quick value retrieval by key and replaced less type-safe approaches.

Problem

The non-obvious difference between Dictionary and Hashtable can lead to potential errors when searching for a missing key. For instance, accessing a key that does not exist throws an error in some instances but not in others. Differences in type safety and performance also lead to inappropriate collection choices.

Solution

In modern versions of VB.NET, best practice is to use Dictionary(Of TKey, TValue), where TKey and TValue are strictly typed:

Dim dict As New Dictionary(Of String, Integer)() dict.Add("apple", 1) dict.Add("banana", 2) If dict.ContainsKey("banana") Then Console.WriteLine(dict("banana")) ' Outputs 2 End If ' Safe value retrieval Dim value As Integer If dict.TryGetValue("cherry", value) Then Console.WriteLine(value) Else Console.WriteLine("Key not found!") End If

Key Features:

  • Dictionary(Of TKey, TValue) operates faster, is type-safe by default, and allows arbitrary keys (if GetHashCode/Equals is correctly implemented).
  • Hashtable accepts and returns Object, does not support generics, is slower, and less convenient in modern code.
  • Accessing a missing key through a dictionary throws a KeyNotFoundException. Use ContainsKey or TryGetValue for safe access.

Trick Questions.

Can Object be used as a key without restrictions in Dictionary?

Formally yes, but proper implementation of the GetHashCode and Equals methods in the class that serves as the key is required. Otherwise, collisions and search errors may occur.

What happens if you access a non-existent key via dict("foo")?

A KeyNotFoundException will be thrown. Without prior key checking (via ContainsKey or TryGetValue), the program will crash.

Do Dictionary and Hashtable support the same order of elements?

No. Both classes do not guarantee the order of element addition. To preserve order, use SortedDictionary, OrderedDictionary, or other structures.

Common Errors and Anti-Patterns

  • Accessing dictionary values without checking for key existence.
  • Using Hashtable in modern code without urgent necessity — absence of type safety.
  • Using objects with unstable hash codes as keys (e.g., mutable objects).

Real-Life Example

Negative Case

A developer chose Hashtable to store large amounts of user data. Hard-to-trace errors occurred — the same key did not always match, Object.ReferenceEquals errors surfaced in tests, and an unstable key type was used.

Pros:

  • Fast prototyping, minimally functioning solution.

Cons:

  • Difficult to debug.
  • "Object" type collection — hard to trace type errors.
  • TryGetValue does not work.

Positive Case

Switched to Dictionary(Of Guid, User): strict typing, supported TryGetValue, eliminated collision errors. Performance improved.

Pros:

  • Predictive behavior.
  • No type errors, less time spent on debugging.

Cons:

  • Requires correct implementation of GetHashCode/Equals if a custom key type is used.