Home
Browsez les 647 exemples
Téléchargez les 647 exemples
Téléchargez des chapitres
Achetez sur amazon.fr
Niveau: Débutant/Intermédiaire
ISBN-2-84177-339-6
50 Euros
|
Exemple 15-15 extrait du chapitre
Collections
Exemple 15-14< > Exemple 15-16
Cet exemple peut être compilé avec la ligne de commande: csc.exe /target:exe Exemple_15_15.cs Erreurs: 0 Avertissements: 0 Remarque:
Exemple_15_15.cs
using System.Collections.Generic;
class Personne {
public Personne(string nom, int anneeNaissance) {
m_Nom = nom;
m_AnneeNaissance = anneeNaissance;
}
public override int GetHashCode() {
return m_AnneeNaissance * m_Nom.GetHashCode();
}
public override bool Equals(object o) {
Personne personne = o as Personne;
if (personne != null)
return (personne.GetHashCode() == GetHashCode());
return false;
}
private string m_Nom;
private int m_AnneeNaissance;
}
class Program {
public static void Main() {
Dictionary<Personne,string> dico = new
Dictionary<Personne,string>();
Personne julien = new Personne( "Julien" , 2002);
Personne mathieu = new Personne( "Mathieu" , 2001);
dico.Add( julien, "20 rue Arson" );
dico.Add( mathieu, "90 Rue Barberis" );
bool b = dico.ContainsKey(julien);
// Ici, b vaut true.
}
}
Copyright Patrick Smacchia 2006 2007
|