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 14-34 extrait du chapitre Les mécanismes utilisables dans C#


Exemple 14-33<     > Exemple 14-35


Cet exemple peut être compilé avec la ligne de commande:
csc.exe /target:exe Exemple_14_34.cs
Erreurs: 0 Avertissements: 0
Remarque:


Exemple_14_34.cs
public class Personnes : System.Collections.IEnumerable {
   private class PersonnesEnumerator : System.Collections.IEnumerator {
      private int index = -1;
      private Personnes P;
      public PersonnesEnumerator(Personnes P){ this.P = P; }
      public bool MoveNext() {
         index++;
         return index < P.m_Noms.Length;
      }
      public void Reset() { index = -1; }
      public object Current { get return P.m_Noms[index]; } }
   }
   // La méthode GetEnumerator() de IEnumerable.
   public System.Collections.IEnumerator GetEnumerator(){
      return new PersonnesEnumerator(this);
   }
   string[] m_Noms;
   // Le constructeur qui initialise le tableau.
   public Personnes(params string[] Noms){
      m_Noms = new string[Noms.Length];
      // Copie le tableau.
      Noms.CopyTo(m_Noms, 0);
   }
   // L'indexeur qui retourne le Nom à partir de l'index.
   private string this[int index]{
      get return m_Noms[index]; }
      set { m_Noms[index] = value; }
   }
}
class Program {
   static void Main() {
      Personnes arrPersonnes = new Personnes(
                         "Michel""Christine""Mathieu""Julien");
      foreach (string in arrPersonnes)
         System.Console.WriteLine(s);
   }
}	
Copyright Patrick Smacchia 2006 2007