You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prefer Dictionary<K, V>.TryAddValue(key) over guarded Add(key) #33799
usingSystem;usingSystem.Collections.Generic;namespaceMyNamespace{publicstaticclassMyClass{staticvoidMain(){Dictionary<int,int>d=new();// A) Check the negative returned value// Beforeif(!d.ContainsKey(5)){d.Add(5,6);}// Afterd.TryAdd(5,6);// B) Check the negative returned value and preserve existing logic inside the if body// Beforeif(!d.ContainsKey(5)){d.Add(5,6);Console.WriteLine($"Value: {d[5]}");}// Afterif(d.TryAdd(5,6)){Console.WriteLine($"Value: {d[5]}");}// C) Check the positive returned value and preserve existing logic in the else bodyif there's one// Beforeif(d.ContainsKey(5)){Console.WriteLine($"Value existed: {d[5]}");}else{d.Add(5,6);Console.WriteLine($"Value added: {d[5]}");}// Afterif(!d.TryAdd(5,6)){Console.WriteLine($"Value existed: {d[5]}");}else{Console.WriteLine($"Value added: {d[5]}");}}}}
I'd suggest enabling this analyzer only for scenarios where the associated value is: (a) a literal; or (b) already fully hydrated before the call to ContainsKey. Otherwise you risk a perf regression in scenarios like the following.
if(!dict.ContainsKey(key)){dict[key]=ComputeExpensiveValue();// expensive computation guarded within ContainsKey check}
In theory you could take a factory argument to perform the expensive computation, but now we'd be making callers understand lambdas and closures when all they wanted to do was a simple dictionary access, and I don't think that's a good trade-off for the majority of our developers.
@buyaa-n@CollinAlpert has a PR been merged fixing this? I see there are 3 related issues and more than one related PR but I'm unsure which issues got fixed.
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
carlossanlop commentedon Jan 15, 2021
Suggested severity: Info
Cases to cover:
GrabYourPitchforks commentedon Feb 4, 2021
I'd suggest enabling this analyzer only for scenarios where the associated value is: (a) a literal; or (b) already fully hydrated before the call to
ContainsKey. Otherwise you risk a perf regression in scenarios like the following.In theory you could take a factory argument to perform the expensive computation, but now we'd be making callers understand lambdas and closures when all they wanted to do was a simple dictionary access, and I don't think that's a good trade-off for the majority of our developers.
terrajobst commentedon Feb 4, 2021
Video
2 remaining items
carlossanlop commentedon Jul 6, 2022
@buyaa-n @CollinAlpert has a PR been merged fixing this? I see there are 3 related issues and more than one related PR but I'm unsure which issues got fixed.
buyaa-n commentedon Jul 6, 2022
Yes this one is fixed
buyaa-n commentedon Jul 22, 2022
Oops, mistaken with #33798, it is not done, reopening