Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions

| public class Contact { private int _ContactID; public int ContactID { get { return _ContactID; } set { _ContactID = value; } } private string _ContactName; public string ContactName { get { return _ContactName; } set { _ContactName = value; } } private Nullable<int> _ContactAge; public Nullable<int> ContactAge { get { return _ContactAge; } set { _ContactAge = value; } } public Contact() { } public Contact(int contactID, string contactName, Nullable<int> contactAge) { this.ContactID = contactID; this.ContactName = contactName; this.ContactAge = contactAge; } public override string ToString() { return "ContactID = " + ContactID.ToString() + ",Contactname = " + ContactName + ",ContactAge = " + ContactAge.ToString() ; } } |
| public class ContactCollection : CollectionBase { public Contact this[int index] { get { return (Contact) this.List[index]; } } public Contact this[string contactName] { get { return this.Find(contactName); } } public void Add(Contact c) { this.List.Add(c); } private Contact Find(string contactName) { foreach (Contact c in this) if (c.ContactName == contactName) return c; return null; } } |
| public class ContactCollection : List<Contact> { public Contact this[string contactName] { get { return this.Find(delegate(Contact c) { return c.ContactName == contactName; }); } } } |
| ContactCollection Contacts = new ContactCollection(); private void Form1_Load(object sender, EventArgs e) { Contacts.Add(new Contact(1, "Romagny", 31)); Contacts.Add(new Contact(2, "Bellin",25)); Contacts.Add(new Contact(3, "Durand", 50)); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(Contacts["Romagny"].ToString()); } private void button2_Click(object sender, EventArgs e) { MessageBox.Show(Contacts[2].ToString()); } |
| public Contact get_Item(string contactName) { return this.Find(contactName); } public Contact get_Item(int index) { return (Contact)base.List[index]; } |