44

In eclipse, when I code in Java, there is a feature to auto-generate a basic, efficient, and bug free implementation of hashCode() and equals() without consuming brain power.

Is there a similar feature either built-in in Visual Studio or in ReSharper ?

Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90

4 Answers4

63

Yes, Resharper can do that. With cursor inside your type, open the “Generate code” menu (Alt+Ins depending on settings or Resharper -> Edit -> Generate Code), and select “Equality members”:

Generate code menu

This opens a window where you can select which members are used for equality, along with some options about the generated code (e.g. should your type implement IEquatable<T>):

Generate equality members window

If you start with a simple type with two properties:

class Person
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
}

Then the generated code may look something like:

class Person : IEquatable<Person>
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    public bool Equals(Person other)
    {
        if (ReferenceEquals(null, other))
            return false;
        if (ReferenceEquals(this, other))
            return true;
        return string.Equals(FirstName, other.FirstName) && string.Equals(LastName, other.LastName);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
            return false;
        if (ReferenceEquals(this, obj))
            return true;
        if (obj.GetType() != this.GetType())
            return false;
        return Equals((Person)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((FirstName != null ? FirstName.GetHashCode() : 0) * 397) ^ (LastName != null ? LastName.GetHashCode() : 0);
        }
    }
}
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
svick
  • 236,525
  • 50
  • 385
  • 514
6

Since you asked if also Visual Studio can do that: since XI.2017 it finaly can generate something useful.

Using ctr+. inside class and choosing "Generate Equals and GetHashCode"

See https://stackoverflow.com/a/48441971/4547594

Igand
  • 1,161
  • 1
  • 15
  • 25
3

The Real Solution to use the Visual Studio itself. There is a built in mechanism, which can generate these functions:

  1. Place your cursor somewhere on the line of your type declaration.
  2. Click the screwdriver icon that appears in the left margin.
  3. Select Generate Equals(object) or Generate Equals and GetHashCode from the drop-down menu.

Description with pictures: https://learn.microsoft.com/en-us/visualstudio/ide/reference/generate-equals-gethashcode-methods?view=vs-2019#how-to

GregC
  • 7,737
  • 2
  • 53
  • 67
György Gulyás
  • 1,290
  • 11
  • 37
  • If your type is implementing something (Interface, base class etc.) you need to put your cursor **before** ':' in the type declaration otherwise the 'Generate Equals and GetHashCode' menu option isn't available. I put my cursor next to my type name & only then did the menu option appear for me. – Carl Heinrich Hancke Nov 19 '20 at 19:11
0

You can use C# IEquitable interface which will auto generate those methods for you.

Pubu
  • 386
  • 1
  • 2
  • 9