简体   繁体   中英

New ReSharper Equals() and GetHashCode() code generation method

I have used the ReSharper feature that allows code generation for a while now. The way I've always done it is by pressing Alt-Enter in my class (outside of any method), or pressing Alt-Ins, and choosing Equality members , then selecting all members and clicking ok.

The code that would have been generated when doing that was something like this:

   public class Foo
   {
      public int Bar { get; }

      public int Baz { get; }

      protected bool Equals(Foo other)
      {
         return Bar == other.Bar
                && Baz == other.Baz;
      }

      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((Foo) obj);
      }

      public override int GetHashCode()
      {
         unchecked
         {
            return (Bar * 397) ^ Baz;
         }
      }
   }

Recently though (maybe with the release of ReSharper 2017 or of a minor version?), pressing Alt-Enter brings another menu, with a Generate Equals and GetHashCode item, which generates this kind of code instead:

   public class Foo
   {
      public int Bar { get; }

      public int Baz { get; }

      public override bool Equals(object obj)
      {
         var foo = obj as Foo;
         return foo != null &&
                Bar == foo.Bar &&
                Baz == foo.Baz;
      }

      public override int GetHashCode()
      {
         var hashCode = 997021164;
         hashCode = hashCode * -1521134295 + Bar.GetHashCode();
         hashCode = hashCode * -1521134295 + Baz.GetHashCode();
         return hashCode;
      }
   }

Weirdly, pressing Alt-Insert still generates the same code than it did before. It seems now ReSharper generates two different templates depending on the way we generate it. The differences seem quite significant, with different prime numbers used, and the loss of the unchecked keyword.

I was familiar with the reasons to use the prime number 397 in that first version, but I cannot find any explanation for the second version of it, not even in the ReSharper online help. The feed integer (997021164 in this example) also seems to change depending on the number of properties to be compared.

Does anyone have an explanation for this?

ReSharper still uses number 397 in GetHashCode generation. Looks like you used Roslyn implementation of "Equals" generation.

If you call Alt + Ins and select "Equality members", ReSharper 2017.x will still generate expected code.

在此处输入图片说明

However, if you hit Alt + Enter to call Alt+Enter menu, not on a method or ReSharper squiggle, this menu will show Roslyn suggestion (VS icons on a light bulb icon)

在此处输入图片说明

Such quick-fix will generate Roslyn implementation.

I might untick ReSharper | Options | Code Inspection | Settings | Visual Studio Integration | Do not show Visual Studio bulb ReSharper | Options | Code Inspection | Settings | Visual Studio Integration | Do not show Visual Studio bulb ReSharper | Options | Code Inspection | Settings | Visual Studio Integration | Do not show Visual Studio bulb checkbox not to merge Roslyn suggestions to ReSharper menu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM