Skip to content

Instantly share code, notes, and snippets.

@canton7
Last active May 5, 2018 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save canton7/72abf244ad5e3f623f71 to your computer and use it in GitHub Desktop.
Save canton7/72abf244ad5e3f623f71 to your computer and use it in GitHub Desktop.
public class Foo : IEquatable<Foo>
{
public int Bar { get; set; }
public int Baz { get; set; }
public override bool Equals(object obj)
{
return this.Equals(obj as Foo);
}
public bool Equals(Foo other)
{
if (Object.ReferenceEquals(other, null))
return false;
if (Object.ReferenceEquals(this, other))
return true;
return this.Bar.Equals(other.Bar) &&
this.Baz.Equals(other.Baz);
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + this.Bar.GetHashCode();
hash = hash * 23 + this.Baz.GetHashCode();
return hash;
}
}
}
public struct Foo : IEquatable<Foo>
{
public int Bar { get; set; }
public int Baz { get; set; }
public override bool Equals(object obj)
{
return (obj is Foo) && this.Equals((Foo)obj);
}
public bool Equals(Foo other)
{
return this.Bar.Equals(other.Bar) &&
this.Baz.Equals(other.Baz);
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + this.Bar.GetHashCode();
hash = hash * 23 + this.Baz.GetHashCode();
return hash;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment