Skip to content

Instantly share code, notes, and snippets.

@marcominerva
Created March 27, 2020 09:41
Show Gist options
  • Save marcominerva/4fe3847ba4b80002dfa9c00dd84dd7f9 to your computer and use it in GitHub Desktop.
Save marcominerva/4fe3847ba4b80002dfa9c00dd84dd7f9 to your computer and use it in GitHub Desktop.
Equality operator for C#
public class Position : IEquatable<Position>
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public bool Equals([AllowNull] Position other)
=> (Latitude, Longitude) == (other?.Latitude, other?.Longitude);
public override bool Equals(object obj) => Equals(obj as Position);
public override int GetHashCode() => HashCode.Combine(Latitude, Longitude);
public static bool operator ==(Position x, Position y)
=> ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(Position x, Position y) => !(x == y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment