Topic Overview

Welcome to our exploration of sorted dictionaries using custom classes and comparers in C#. In today's lesson, we'll learn how to use custom classes as keys in sorted dictionaries. This approach enhances data organization and access. With the addition of comparers, we can dictate the order in such dictionaries.

Quick Recap on Sorted Dictionaries

A sorted dictionary is a dictionary with its keys always in order. This arrangement makes operations like searching for keys within a range more efficient. In C#, we use the SortedDictionary class to create them:

Introduction to Custom Classes in C#

Custom classes enable us to create objects that fit our data — for instance, a Person class for employee information or a Book class for a library database. In C#, classes are the blueprints for creating objects.

Consider this simple class, for example:

Using Custom Classes as Keys in Sorted Dictionaries

Using custom classes as dictionary keys helps organize complex multivariate keys in a sorted dictionary. Consider the following example using the Person class as a key in a sorted dictionary (i.e., SortedDictionary). However, this will not work yet.

We can see here that John is assigned the value "Programmer", and Alice is assigned the value "Designer." However, this code will produce an exception. The reason is that the SortedDictionary needs a way to compare the Person objects to maintain its order. This requires not only implementing GetHashCode and Equals (important for identifying objects) but also providing a means to compare objects, typically through the IComparable<T> interface or a custom IComparer<T>.

Comparers and Their Role in Sorted Dictionaries

C# uses a comparer to determine the order of two keys. To make this comparison, we add the IComparable interface or a custom IComparer to our class. Without these methods, SortedDictionary can't compare its Person class keys. Here’s how to modify the Person class to implement the IComparable interface:

In the code above, we implement the IComparable interface and override the CompareTo, Equals, and GetHashCode methods. The CompareTo method ensures that Person objects are initially sorted by age, and if ages are the same, then by name. Overriding Equals and GetHashCode ensures that Person objects behave consistently when used in collections like SortedDictionary. The GetHashCode method returns an integer representation of an object for quick look-ups in hash-based collections.

Lesson Summary

We've explored how to use custom classes as keys in sorted dictionaries and how comparers work in this context using the IComparable interface. Implementing CompareTo is necessary to maintain the order within the SortedDictionary, while overriding Equals and GetHashCode ensures object consistency and identity in collections. Now, prepare for some hands-on exercises to reinforce these concepts.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal