public class PeopleCollection : IEnumerable
{
private Dictionary<string, Person> listPeople = new Dictionary<string, Person>();
// This indexer returns a person based on a string index.
public Person this[string name]
{
get {
return (Person)listPeople[name]; // **Is this cast necessary?**
}
set {
listPeople[name] = value;
}
}
..
}
Question: should we cast the return value from listPeople[name] to Person?
Thank you
No, it is not needed. In
Dictionary<TKey, TValue>the indexer is declared as:For the
Dictionary<string, Person>in thelistPeoplefield,TValueis replaced withPerson, so the indexer will return a reference to aPersonobject.