O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

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

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 ...

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