Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| private void Form1_Load(object sender, EventArgs e) { oContacts = new ContactCollection(); oContacts.Add(new Contact(1, "Dupond", "Julien", "jdupond@hotmail.fr")); oContacts.Add(new Contact(2, "Martin", "Pierre", "pmartin@hotmail.fr")); oContacts.Add(new Contact(3, "Bellin", "Marie", "mb3@yahoo.fr")); oContacts.Add(new Contact(4, "Durand", "Paul", "durand1006@voila.fr")); oContacts.Add(new Contact(5, "Suisse", "André", "andre05@voila.fr")); } |
| private bool StartWith(Contact oContact) { bool bResult = false; if (oContact.Name.StartsWith("D")) { bResult = true; } else { bResult = false; } return bResult; } |
| using System; using System.Collections.Generic; using System.Text; namespace EtudeCollectionsGeneriques { public class ContactCollection : System.Collections.Generic.List<Contact> { } } |
| using System; using System.Collections.Generic; using System.Text; namespace EtudeCollectionsGeneriques { public class Contact : IComparable<Contact> { private int _ID; private string _Name; private string _FirstName; private string _Email; public Contact() { } public Contact(int ID, string Name, string FirstName, string Email) { this.ID = ID; this.Name = Name; this.FirstName = FirstName; this.Email = Email; } public int ID { get { return _ID; } set { _ID = value; } } public string Name { get { return _Name; } set { _Name = value; } } public string FirstName { get { return _FirstName; } set { _FirstName = value; } } public string Email { get { return _Email; } set { _Email = value; } } public override string ToString() { return this.ID.ToString() + " " + this.Name + " " + this.FirstName + " " + this.Email; } public class ContactNameComparer : System.Collections.Generic.IComparer<Contact> { public SorterMode SorterMode; public ContactNameComparer() { } public ContactNameComparer(SorterMode SorterMode) { this.SorterMode = SorterMode; } #region IComparer<Contact> Membres int System.Collections.Generic.IComparer<Contact>.Compare(Contact x, Contact y) { if (SorterMode == SorterMode.Ascending) { return y.Name.CompareTo(x.Name); } else { return x.Name.CompareTo(y.Name); } } #endregion } #region IComparable<Contact> Membres public int CompareTo(Contact other) { return this.ID.CompareTo(other.ID); } #endregion } } |
| // 1 on crée un objet Contact oContact; oContact=new Contact(9,"Leneuf","Jean-pierre","neuf9@hotmail.com"); // 2 on ajoute l'objet à la collection oContacts.Add(oContact); |
| // 1 on crée un objet Contact oContact; oContact=new Contact();// oContact.ID = 9; oContact.Name = "Leneuf"; oContact.FirstName = "Jean-pierre"; oContact.Email = "neuf9@hotmail.com"); // 2 on ajoute l'objet à la collection oContacts.Add(oContact); |
| oContacts = new ContactCollection(); oContacts.Add(new Contact(1, "Dupond", "Julien", "jdupond@hotmail.fr")); oContacts.Add(new Contact(2, "Martin", "Pierre", "pmartin@hotmail.fr")); |
| // insérer un element à l'index spécifié oContacts.Insert(2, new Contact(8, "Perrot", "Alphonse", "aperrot@hotmail.fr")); |
| // 1 on crée une collection ContactCollection oRangeContacts; oRangeContacts = new ContactCollection(); oRangeContacts.Add(new Contact(6, "Cossin", "Luc", "cossinluc@laposte.net")); oRangeContacts.Add(new Contact(7, "Mars", "Julie", "mars13@hotmail.fr")); // 2 on ajoute la collection à la collection oContacts.AddRange(oRangeContacts); |
| // ContactCollection oRangeContacts; oRangeContacts = new ContactCollection(); oRangeContacts.Add(new Contact(6, "Cossin", "Luc", "cossinluc@laposte.net")); oRangeContacts.Add(new Contact(7, "Mars", "Julie", "mars13@hotmail.fr")); // On insère à la position 2 les nouveaux éléments oContacts.InsertRange(2, oRangeContacts); |
| Contact oContact = oContacts.Find(StartWith); oContact.Name = "Gaston"; |
| oContacts[2].Name = "Gaston"; |
| oContacts.Remove(oContacts.FindLast(StartWith)); |
| oContacts.RemoveAt(1); |
| oContacts.RemoveAll(StartWith); |
| // supprime 3 contacts à partir de l’index 2 de la collection de contacts oContacts.RemoveRange(2, 3); |
| // 6 Clear() : supprime tous les éléments de la collection oContacts.Clear(); |
| foreach (Contact oContact in oContacts) { } |
| for (int nCount = 0; nCount <= oContacts.Count - 1; nCount++) { } |
| string smessage = string.Empty; IEnumerator<Contact> oContactsEnumerator = oContacts.GetEnumerator(); while (oContactsEnumerator.MoveNext()) { smessage += oContactsEnumerator.Current.ToString() + Environment.NewLine; } |