FROM
Persons["John"];
you use an indexer for any type on which the underlying object resides, in this case List. What you want is possible in two ways and maybe more, but these are my ideas at the moment:
The first approach: you can create a special collection list (say, PersonList: List) that overrides the indexer and passes an instance of Company in the constructor, so that you can request a client instance in the override implementation.
public class PersonList: List<Person> { public new Person this[string name] { get { return company.GetMyPerson(name); } } }
Second approach: you create an indexer on a company class directly and have something like c ["John"]
Now about these approaches β The first one looks better from my point of view, because it violates the smaller design principles. It is not very pleasant to request a company using an indexer for people in the second approach ...
source share