Sys*_*gon 4 .net c# resharper dictionary code-inspection
Resharper 抛出此检查问题:
添加到集合之前的冗余字典“ContainsKey”
在这部分代码中:
var userPref = new Preferences { Key = key, Value = value };
if (this.preferencesDictionary.ContainsKey(key))
{
this.preferencesDictionary[key] = userPref;
}
else
{
this.preferencesDictionary.Add(key, userPref);
}
return this;
Run Code Online (Sandbox Code Playgroud)
我真的不认为这是多余的。这是一个我应该跳过的错误?或者这里真的有改进吗?
当未找到键时,索引器的 getterDictionary<TKey, TValue>会抛出 a KeyNotFoundException,而如果未找到键,则 setter 会将值插入到字典中。
请参阅此处的文档。
适当的价值
与指定键关联的值。如果未找到指定的键,则 get 操作会抛出
KeyNotFoundException,并且 set 操作会使用指定的键创建一个新元素。
preferencesDictionary[key] 看起来你在这里调用了 getter,但由于它后面跟着赋值运算符=,所以你实际上是在调用 setter。
| 归档时间: |
|
| 查看次数: |
347 次 |
| 最近记录: |