【发布时间】:2020-08-07 14:06:37
【问题描述】:
我有一个 Dictionary 的键类型 string 和 List<int> 的值,我需要在保持 List<int> 值的同时更改键 string 值。
static void ChangeName(Dictionary<string, List<int>> studentGrades)
{
Console.WriteLine("Please enter the student whose name you wish to change.");
string input = Console.ReadLine();
if (studentGrades.ContainsKey(input))
{
Console.WriteLine("What would you like to change " + input + " to?");
string newname = Console.ReadLine();
studentGrades[input] = newname;
}
}
我知道执行studentGrades[input] = newname; 不起作用,因为它试图更改List<int> 的值。我也不确定是否可以更改键的值,有哪些可能的解决方法?我在Visual Studio 中收到错误CS0029。
【问题讨论】:
标签: c# list dictionary key