From be99c8d3f92e80527b3b230761c64d9dabf4aec2 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 22:18:37 +0200 Subject: [PATCH] [FIX] fixed equals operators --- SharedClientServer/User.cs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/SharedClientServer/User.cs b/SharedClientServer/User.cs index 4a1a36c..8d45dc2 100644 --- a/SharedClientServer/User.cs +++ b/SharedClientServer/User.cs @@ -1,11 +1,12 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Text; namespace SharedClientServer { - class User + class User : IEquatable { private string _username; private int _score; @@ -29,12 +30,20 @@ namespace SharedClientServer public static bool operator ==(User u1, User u2) { - return u1.Username == u2.Username; + if (object.ReferenceEquals(u1, null)) + { + return object.ReferenceEquals(u2, null); + } + return u1.Equals(u2 as object); } public static bool operator !=(User u1, User u2) { - return u1.Username != u2.Username; + if (object.ReferenceEquals(u1, null)) + { + return object.ReferenceEquals(u2, null); + } + return u1.Equals(u2 as object); } public override bool Equals(object obj) @@ -45,11 +54,15 @@ namespace SharedClientServer } else { - User other = obj as User; - return other.Username == this.Username; + return this.Equals(obj as User); + } } - + + public bool Equals([AllowNull] User other) + { + return other.Username == this.Username; + } public string Username {