// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class Person { private int id; private string name; private string address; public Person(int id, string name, string address) { this.id = id; this.name = name; this.address = address; } public int Id { get { return id; } } public string Name { get { return name; } set { name = value; } } public string Address { get { return address; } set { address = value; } } public override string ToString() { return ("[" + id + "," + name + "," + address + "]"); } } public class PersonMultiContainer { private IList simpleList; private IDictionary mapFromId; private IDictionary mapFromName; public PersonMultiContainer() { simpleList = new List(); mapFromId = new Dictionary(); mapFromName = new Dictionary(); } public void Add(Person p) { simpleList.Add(p); mapFromId.Add(p.Id, p); mapFromName.Add(p.Name, p); } public IList SimpleList { get { return simpleList; } } public Person LookupById(int id) { return mapFromId[id]; } public Person LookupByName(string name) { return mapFromName[name]; } } public class MainClass { public static void Main(string[] args) { PersonMultiContainer pmc = new PersonMultiContainer(); pmc.Add(new Person(1, "Anders", "A vej #1")); pmc.Add(new Person(2, "Benny", "BB vej #2")); pmc.Add(new Person(3, "Christian", "CCC vej #3")); foreach(Person p in pmc.SimpleList) { Console.WriteLine(p); } Console.WriteLine(pmc.LookupById(3)); Console.WriteLine(pmc.LookupByName("Benny")); } } }