4

I'm wondering if there is a way to put all dictionary values to null (or each elements) ? Because we can't iterate on it if the values has been changed to null.

(I've searched on Stackoverflow before asking this simple question but I didn't find a clear response on this issue).

Regards

4

1 回答 1

11

这是你想要的吗?

foreach(var key in dict.Keys.ToList())
    dict[key] = null;

重要提示:调用ToList(). 这会将密钥复制到临时列表中。否则,您将收到一条InvalidOperationException消息:

收藏已修改;枚举操作可能无法执行。

于 2012-09-24T15:38:40.033 回答