// ======================================================================== // // Copyright (C) 2016-2017 MARC CHEVALDONNE // marc.chevaldonne.free.fr // // Module : Nounours.cs // Author : Marc Chevaldonné // Creation date : 2016-10-01 // // ======================================================================== using System; namespace ex_023_018_ReadOnlyDictionaries { public class Nounours : IEquatable { public int Id { get; private set; } public string Nom { get; set; } public DateTime DateDeNaissance { get; private set; } public int NbPoils { get; set; } public Nounours(int id, string nom, DateTime dateDeNaissance, int nbPoils) { Id = id; Nom = nom; DateDeNaissance = dateDeNaissance; NbPoils = nbPoils; } //... /// /// returns a hash code in order to use this class in hash table /// /// hash code public override int GetHashCode() { return (DateDeNaissance.Year * 1000 + DateDeNaissance.DayOfYear) * Nom.GetHashCode(); } /// /// checks if the "right" object is equal to this Nounours or not /// /// the other object to be compared with this Nounours /// true if equals, false if not public override bool Equals(object right) { //check null if (object.ReferenceEquals(right, null)) { return false; } if (object.ReferenceEquals(this, right)) { return true; } if (this.GetType() != right.GetType()) { return false; } return this.Equals(right as Nounours); } /// /// checks if this Nounours is equal to the other Nounours /// /// the other Nounours to be compared with /// true if equals public bool Equals(Nounours other) { return (this.Nom.Equals(other.Nom) && this.DateDeNaissance.Equals(other.DateDeNaissance)); } } }