更改字典键的值

C S*_*per 0 .net c#

我有一个字符串列表.

我想在其中删除一个如下:

  settings.RecentSearches.Keys.Remove(itemToAdd.Key);
  settings.RecentSearches.Keys.Add(itemToAdd.Key);
Run Code Online (Sandbox Code Playgroud)

错误:

Mutating a key collection derived from a dictionary is not allowed.
Run Code Online (Sandbox Code Playgroud)

我想在其中添加和删除值.

我能做什么?

ken*_*n2k 6

您无法从字典的键集合中删除项目.您必须使用Dictionary.Remove从字典本身中删除该项.

  • @LOLSinger确实如此.如果从字典中删除项目,则相关键也将被删除. (2认同)