Skip to content

Generate Equals() and GetHashCode() for reference types #50

@brenoferreira

Description

@brenoferreira

Generate Equals and GetHashCode methods for reference types, similar to what Resharper does https://www.jetbrains.com/resharper/documentation/help20/AdvEditing/generateEquals.html and following the guidelines in http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

Only reference types. For value types see #60.

This:

class MyClass
{
  public int Prop1 { get; private set; } 
  public float Prop2 { get; private set; }
}

Becomes:

class MyClass
{
  public int Prop1 { get; private set; } 
  public float Prop2 { get; private set; }

  public bool Equals(MyClass other) 
  {
    if(other == null) {return false; }
    return other.Prop1 == Prop1 && other.Prop2 == Prop2; 
  }

  public bool Equals(Object other) {
    if(other == null) { return false; } 
    var obj = other as MyClass;
    if(obj == null) { return false; } 
    return Equals(obj);
  }

  public override int GetHashCode() 
  { 
    unchecked 
    {
        int hash = (int) 2166136261;
        hash = hash * 16777619 ^ Prop1.GetHashCode();
        hash = hash * 16777619 ^ Prop2.GetHashCode();
        return hash;
    }
  }
}

This is essentially a refactoring, so we are classifying it as a Hidden diagnostic.
Diagnostic id: CC0059
Category: Refactoring
See references on the comments below.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions