-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefer Dictionary<K, V>.TryGetValue() over guarded indexer access #33798
Labels
api-approved
API was approved in API review, it can be implemented
area-System.Collections
code-analyzer
Marks an issue that suggests a Roslyn analyzer
code-fixer
Marks an issue that suggests a Roslyn code fixer
Milestone
Comments
terrajobst
added
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Collections
code-analyzer
Marks an issue that suggests a Roslyn analyzer
labels
Mar 19, 2020
Dotnet-GitSync-Bot
added
the
untriaged
New issue has not been triaged by the area owner
label
Mar 19, 2020
jeffhandley
added
the
code-fixer
Marks an issue that suggests a Roslyn code fixer
label
Mar 21, 2020
Estimates:
|
terrajobst
removed
the
untriaged
New issue has not been triaged by the area owner
label
Mar 21, 2020
Suggested severity: info // Before
public int TryGet(Dictionary<string, int> data, string key)
{
if (data.ContainsKey(key))
{
return data[key];
}
return 0;
}
// After
public int TryGet(Dictionary<string, int> data, string key)
{
if (data.TryGetValue(key, out int value))
{
return value;
}
return 0;
} It may be possible to preserve unrelated code inside the // Before
public int TryGet(Dictionary<string, int> data, string key)
{
if (data.ContainsKey(key))
{
Foo(data[key]); // Unrelated code
return data[key];
}
return 0;
}
// After
public int TryGet(Dictionary<string, int> data, string key)
{
if (data.TryGetValue(key, out int value))
{
Foo(value); // Unrelated code
return value;
}
return 0;
} Avoid triggering if the data is modified before returning: public int DontTrigger(Dictionary<string, int> data, string key)
{
if (data.ContainsKey(key))
{
data[key] = DoSoemthing(data[key]); // Unrelated code
return data[key];
}
return 0;
} |
carlossanlop
added
api-ready-for-review
API is ready for review, it is NOT ready for implementation
and removed
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
labels
Jan 15, 2021
terrajobst
added
api-approved
API was approved in API review, it can be implemented
and removed
api-ready-for-review
API is ready for review, it is NOT ready for implementation
labels
Feb 4, 2021
|
I'd like to do this one. @carlossanlop |
carlossanlop
removed
the
help wanted
[up-for-grabs] Good issue for external contributors
label
Feb 8, 2021
It's yours, @CollinAlpert . Thanks for offering your help! |
Closed
18 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
api-approved
API was approved in API review, it can be implemented
area-System.Collections
code-analyzer
Marks an issue that suggests a Roslyn analyzer
code-fixer
Marks an issue that suggests a Roslyn code fixer
Dictionary<K, V>.ContainsKey(key)
followed byDictionary<K, V>.this[key]
can be combined into a singleTryGetValue
call.Category: Performance
The text was updated successfully, but these errors were encountered: