Skip to content

Instantly share code, notes, and snippets.

@martindevans
Created October 13, 2018 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martindevans/4dd95653e09acc37943b39ab1b953b82 to your computer and use it in GitHub Desktop.
Save martindevans/4dd95653e09acc37943b39ab1b953b82 to your computer and use it in GitHub Desktop.
public class Cat
: IEquatable<Cat>
{
public readonly string Name;
public readonly int Age;
public Cat(string name, int age)
{
Name = name;
Age = age;
}
public bool Equals(Cat other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return string.Equals(Name, other.Name) && Age == other.Age;
}
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((Cat)obj);
}
public override int GetHashCode()
{
unchecked
{
return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ Age;
}
}
public static bool operator ==(Cat left, Cat right)
{
return Equals(left, right);
}
public static bool operator !=(Cat left, Cat right)
{
return !Equals(left, right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment