I want to retrieve the phone number for Bill from phoneBook.
class PersonsName
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
// Dictionary Class, add contacts to phone book
Dictionary<PersonsName, int> phoneBook = new Dictionary<PersonsName, int>()
{
{new PersonsName {FirstName = "Bill", LastName = "Gates" }, 5550100 },
{new PersonsName {FirstName = "Mark", LastName = "Zuckerberg" }, 5551438 }
};
Why does the following give me an exception where the key is not found? How can I retrieve the phone number without looping through the dictionary?
PersonsName personA = new PersonsName { FirstName = "Bill", LastName = "Gates" };
int billssNumber = phoneBook[personA]; //key not found