Lo and behold, since November 2017 even Visual Studio itself can generate meaningful implementation of these methods (at least since version 15.5.2).
Just press ctrl+. or right click inside the class and choose "Quick Actions" and then "Generate Equals and GetHashCode"
Docs for the feature:
https://docs.microsoft.com/en-us/visualstudio/ide/reference/generate-equals-gethashcode-methods
public class Foo
{
public Bar Bar { get; set; }
public string FooBar { get; set; }
public override bool Equals(object obj)
{
var foo = obj as Foo;
return foo != null &&
EqualityComparer<Bar>.Default.Equals(Bar, foo.Bar) &&
FooBar == foo.FooBar;
}
public override int GetHashCode()
{
var hashCode = 181846194;
hashCode = hashCode * -1521134295 + EqualityComparer<Bar>.Default.GetHashCode(Bar);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FooBar);
return hashCode;
}
}
Update: Note tough, that you still might not want to trust VS completely and test Equals, since if your class contains Collection, Equals will depend on reference equality once more as this term is used:
EqualityComparer<IList<Foo>>.Default.Equals(SomeFoos, other.SomeFoos);
OMG anyone?
And ReSharper does that too.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…