下面是给出的一个相应实现的示例代码: ```csharp using System; using System.Collections.Generic; namespace phone { class Program { static void Main(string[] args) { Dictionary<string, City> phoneBook = new Dictionary<string, City>(); // 添加5个电话实例到Dictionary City city1 = new City("010", "北京"); phoneBook.Add(city1.AreaCode, city1); City city2 = new City("021", "上海"); phoneBook.Add(city2.AreaCode, city2); City city3 = new City("0755", "深圳"); phoneBook.Add(city3.AreaCode, city3); City city4 = new City("027", "武汉"); phoneBook.Add(city4.AreaCode, city4); City city5 = new City("020", "广州"); phoneBook.Add(city5.AreaCode, city5); Console.WriteLine("请输入电话号码:"); string phoneNumber = Console.ReadLine(); bool areaCodeExists = false; foreach (var kvp in phoneBook) { if (phoneNumber.StartsWith(kvp.Key)) { Console.WriteLine($"电话号码 {phoneNumber} 属于城市 {kvp.Value.CityName}"); areaCodeExists = true; break; } } if (!areaCodeExists) { Console.WriteLine("该区号不存在"); } } } class City { public string AreaCode { get; private set; } public string CityName { get; private set; } public City(string areaCode, string cityName) { AreaCode = areaCode; CityName = cityName; } } } ``` 注意,上述代码是一个简化的示例,只添加了5个城市的数据,如果你想要更多城市的电话号码数据,只需要继续按照相同的格式添加到Dictionary中即可。 [2024-06-02 20:24:19 | AI写代码神器 | 1243点数解答]