Skip to content

Instantly share code, notes, and snippets.

@lomholdt
Last active September 11, 2018 18:27
Show Gist options
  • Save lomholdt/a6335a033e68a759cf9700d6de3bb6e7 to your computer and use it in GitHub Desktop.
Save lomholdt/a6335a033e68a759cf9700d6de3bb6e7 to your computer and use it in GitHub Desktop.
Overriden == operator
class Person
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public Person(string firstName, string lastName) => (FirstName, LastName) = (firstName, lastName);
public static bool operator ==(Person a, Person b)
{
// This creates funky results
if (object.ReferenceEquals(b, null))
{
return true;
}
return a.FirstName == b.FirstName && a.LastName == b.LastName;
}
public static bool operator !=(Person a, Person b)
{
return !(a == b);
}
public override bool Equals(object other)
{
return other is Person && this == (Person)other;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
if (object.ReferenceEquals(this, null))
{
return "null";
}
return $"{this.FirstName} {this.LastName}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment