求高手指点,下面这段泛型集合代码为什么修改里面的信息,经过查询之后查询不出来修改后的信息
using System;using System.Collections.Generic;
using System.Text;
namespace lesson10_3
{
class Program
{
static void Main(string[] args)
{
//创建一个Dictionary<TKey, TValue>类
Dictionary<int, City> add = new Dictionary<int, City>();
//实例化四个城市类对象
City city1 = new City("仙桃市", 0728);
City city2 = new City("黄石市", 0714);
City city3 = new City("荆州市", 0716);
City city4 = new City("襄樊市", 0710);
City city5 = new City();
//city5._City = "黄冈市";
//city5._Number = 0713;
//将四个学生类对象添加到泛型集合类中
add.Add(city1._Number, city1);
add.Add(city2._Number, city2);
add.Add(city3._Number, city3);
add.Add(city4._Number, city4);
string choice;
do
{
Console.WriteLine("********************************");
Console.WriteLine("******* 1.显示全部信息 *********");
Console.WriteLine("******* 2.增加 ********");
Console.WriteLine("******* 3.删除 ********");
Console.WriteLine("******* 4.查询 *********");
Console.WriteLine("******* 5.修改 ********");
Console.WriteLine("******* 6.退出 ********");
Console.WriteLine("*******************************");
choice = Console.ReadLine();//功能选项
switch (choice)
{
case "1":
Console.WriteLine("全部城市信息为:");
//打印输出全部信息
foreach (City c in add.Values)
{
Console.WriteLine(c.ToString());
}
break;
case "2":
Console.WriteLine("请输入要添加的城市区号:");
city5._Number = Convert.ToInt32(Console.ReadLine());//接受用户输入区号
Console.WriteLine("请输入要添加的城市名称:");
city5._City = Console.ReadLine();//接受用户输入城市
add.Add(city5._Number, city5);//将输入的
Console.WriteLine("已成功添加信息");
break;
case "3":
Console.WriteLine("请输入要删除的城市区号:");
int qh = Convert.ToInt32(Console.ReadLine());
add.Remove(qh);
Console.WriteLine("已成功删除信息。");
break;
case "4":
Console.WriteLine("请输入你要查询的城市区号:");
int str = Convert.ToInt32(Console.ReadLine());
if (add.ContainsKey(str))
{
Console.WriteLine(add[str].ToString());
}
else
{
Console.WriteLine("没有该城市的信息");
}
break;
case "5":
Console.WriteLine("请输入你要修改的城市区号:");
int qh1 = Convert.ToInt32(Console.ReadLine());
if (add.ContainsKey(qh1))
{
Console.WriteLine("修改的城市区号为:");
int str1 = Convert.ToInt32(Console.ReadLine());
add[qh1]._Number = str1;
Console.WriteLine(add[qh1].ToString());
//Console.WriteLine("{1}的区号修改为:{0}", add[qh1]._Number, add[qh1]._City);
}
break;
case "6":
break;
default:
Console.WriteLine("你输入的选项有误!");
break;
}
} while (choice != "6");
if (choice == "6")
{
Console.WriteLine("已退出!!!");
}
Console.ReadKey();
}
}
/// <summary>
/// 城市类
/// </summary>
class City
{
private string city;//城市
private int number;//区号
//无参构造方法
public City()
{
}
//有参构造方法用于赋初值
public City(string city, int number)
{
this.city = city;
this.number = number;
}
public string _City
{
get { return city; }//只读
set { this.city = value; }//只写
}
public int _Number
{
get { return number; }//只读
set { this.number = value; }//只写
}
//重写系统自带的ToString()方法
public override string ToString()
{
return city + "的区号为:" + number;
}
}
}
在下新手没什么分,请各位谅解帮帮我解决哈这个问题