Open wstaelens opened 2 months ago
Diagnostic ID: CA1854 Prefer a "TryGetValue" call over a Dictionary indexer access guarded by a "ContainsKey" check to avoid double lookup
latest .net 8 version and vs pro 2022 17.9.6
public Dictionary<int, Dictionary<int, bool?>> PermissionsData { get; set; } public void Combine(IPermission p) { // ...... some logic if (p == null) return; TypePermission otherP = (TypePermission)p; foreach (int objId in otherP.PermissionsData.Keys) { if (PermissionsData.ContainsKey(objId)) CombineEntityPerms(objId, otherP.PermissionsData[objId]); else PermissionsData.Add(objId, otherP.PermissionsData[objId]); } // some logic... }
it suggest to do a trygetvalue and use value but doesn't see that PermissionsData.ContainsKey(objId) and otherP.PermissionsData[objId] are different.
PermissionsData.ContainsKey(objId)
otherP.PermissionsData[objId]
Analyzer
Diagnostic ID: CA1854 Prefer a "TryGetValue" call over a Dictionary indexer access guarded by a "ContainsKey" check to avoid double lookup
Analyzer source
latest .net 8 version and vs pro 2022 17.9.6
Describe the bug
it suggest to do a trygetvalue and use value but doesn't see that
PermissionsData.ContainsKey(objId)
andotherP.PermissionsData[objId]
are different.