Created
March 27, 2020 09:41
-
-
Save marcominerva/4fe3847ba4b80002dfa9c00dd84dd7f9 to your computer and use it in GitHub Desktop.
Equality operator for C#
This file contains hidden or 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 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