public class InvoiceList { List<Invoice> Invoices = new List<Invoice>(); //Add a Couple of dummy Invoices: public InvoiceList() { Invoices.Add(new Invoice(){Id="A123"}); Invoices.Add(new Invoice(){Id="B345"}); } public Invoice this[string s] { get { Invoice i = Invoices.Find(x => x.Id == s); return i; } } } public class Invoice { public string Id { get; set; } }Given this you can access an Invoice using the Id as an indexer. e.g.:
InvoiceList l = new InvoiceList();
Invoice v = l["A123"];
>I have this customer class:>public class Customer >{ > private List<Invoice> _Invoices = new List<Invoice>(); > public Invoice this[int item] > { > get > { > return _Invoices[item]; > } > set > { > _Invoices[item] = value; > } > } >} >>
>static void Main(string[] args) >{ > Invoice InvA = new Invoice(); > InvA.InvoiceId = "A0001"; > > Invoice InvB = new Invoice(); > InvB.InvoiceId = "B0002"; > > Customer cust = new Customer(); > cust[0] = InvA; > cust[1] = InvB; >} >>