using System; namespace Model.Players { /// /// A player for the purpose of a game of dice, consists of a name ///
/// Two players are equal for Equals() and GetHashCode() if they have the same name, once trimmed and case-insensitive ///
public sealed class Player : IEquatable { /// /// a player's unique username /// public string Name { get; private set; } public Player(string name) { if (!string.IsNullOrWhiteSpace(name)) { Name = name.Trim(); } else throw new ArgumentException("param should not be null or blank", nameof(name)); } /// /// this is a copy contructor /// /// a player object to be copied public Player(Player player) : this(player?.Name) // passes the player's name if player exists, else null { } public override string ToString() { return Name; } public bool Equals(Player other) { return Name.ToUpper() == other.Name.ToUpper(); // equality is case insensitive } public override bool Equals(object obj) { if (obj is not Player) { return false; } return Equals(obj as Player); // casting, to send it to the above Equals() method } public override int GetHashCode() { return Name.ToUpper().GetHashCode(); // hash is case insensitive } } }