Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to auto-generate GetHashCode and Equals with ReSharper?

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 ?

like image 525
Samuel Rossille Avatar asked Feb 01 '13 18:02

Samuel Rossille


People also ask

Should I override GetHashCode?

If you're implementing a reference type, you should consider overriding the Equals method if your type looks like a base type, such as Point, String, BigNumber, and so on. Override the GetHashCode method to allow a type to work correctly in a hash table.

Why to override Equals object and hashCode() method c#?

Why is it important to override GetHashCode ? It s important to implement both equals and gethashcode, due to collisions, in particular while using dictionaries. if two object returns same hashcode, they are inserted in the dictionary with chaining. While accessing the item equals method is used.

How do I override equals in Intellij?

You can use ⌘N (macOS), or Alt+Insert (Windows/Linux) for the Generate menu and then select equals() and hashCode() . You can also use the same shortcut again and select toString() to override that method as well.


2 Answers

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);         }     } } 
like image 197
svick Avatar answered Oct 10 '22 02:10

svick


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

like image 22
Igand Avatar answered Oct 10 '22 04:10

Igand