Visual Studio Generation Shortcut
Instead of cutting, pasting, and refactoring old code to ensure that you create a well-structured GetHashCode()
method whenever you define your own Equals()
method, you can use Visual Studio’s code generation tool! Just right-click in your class file on the class name and follow these prompts.
Let’s use a Course class to demonstrate:
public class Course
{
public string Topic { get; set; }
public Teacher Instructor { get; set; }
public List<Student> EnrolledStudents { get; set; }
}
In Visual Studio, right-click on the class name and select Quick Fix for Mac users or Quick Actions and Refactorings for Windows users from the menu. You can also skip this step by placing your cursor on the line with the class name and clicking on the screwdriver icon to the left.
Select the Generate Equals() and GetHashCode() option from the resulting menu:
A new menu will appear where you can make selections about which class members you want to use for the
Equals()
andGetHashCode()
methods. In the case of equality between twoCourse
objects, we want to establish that twoCourse
objects are equal if the Topic members andInstructor
members are equal. Once you select the necessary members, click Ok!Once you click Ok, Visual Studio generates the
Equals()
andGetHashCode()
methods, resulting in the following class.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public class Course { public string Topic { get; set; } public Teacher Instructor { get; set; } public List<Student> EnrolledStudents { get; set; } public override bool Equals(object obj) { return obj is Course course && Topic == course.Topic && EqualityComparer<Teacher>.Default.Equals(Instructor, course.Instructor); } public override int GetHashCode() { return HashCode.Combine(Topic, Instructor); } }
In order to gain an understanding at what Visual Studio just did for us, review the section on the Equals()
method and take note of the following lines of code in the code block above. While the behavior of the code is the same as the various implementations of the Equals()
methods on the previous page
, Visual Studio’s method does not necessarily look similar to the ones we wrote before.
- On line 9, we see some new syntax. With
is
, the compiler confirms that it is possible to castobj
to a variablecourse
of typeCourse
. If it is, the value ofcourse
is set toobj
. All in one line! - On line 10, the value of
course.Topic
andTopic
is compared for equality. - On line 11, the value of
course.Instructor
and Instructor is compared for equality.
If all of the equality checks on lines 9, 10, and 11 come out to be true, then the generated Equals() method will return true.
Try It!
Try using the Visual Studio shortcut to generate a different method in the Course
class!