Created
October 13, 2018 23:47
-
-
Save martindevans/4dd95653e09acc37943b39ab1b953b82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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