Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
var phones = new Dictionary<string, int>();
phones["Ann"] = 101;
phones["Bob"] = 102;
Console.WriteLine(phones["Ann"]);
Console.WriteLine(phones["ann"]);101, then a KeyNotFoundException
- Expecting
101 0: reading a missing key through the indexer throws aKeyNotFoundException; it does not give the default0. - Expecting
annto findAnn: string keys are compared case-sensitively, so the dictionary has no keyann.
- How would you read the value so that a missing key gives
0instead of an exception? - What happens if the program then calls
phones.Add("Ann", 199)?