19.3. Hashtable

Like a Java's java.util.Hashtable, a .NET Hashtable is a useful data type which encapsulates key/value pairs.

Objects used as keys in a Hashtable must override or rely on the inherited GetHashCode and Equals methods of System.Object because these methods are invoked for matching. Note that the original Equals method in System.Object returns true only if both objects being compared are the same instance created on the heap. Hence, if key equality is simply reference equality, there is no need to override these methods.

The program below demonstrates how Hashtable can be used. The output is interspersed with the code to show the outputs of the different sections.

 1: using System; 2: using System.Collections; 3: 4: public class ...

Get From Java to C#: A Developer's Guide 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.