Skip to content

Commit 53b0980

Browse files
Use TryGetValue() for dictionary lookup (#44791)
Use `TryGetValue()` instead of `ContainsKey()` and the indexer.
1 parent f200c95 commit 53b0980

File tree

3 files changed

+6
-14
lines changed

3 files changed

+6
-14
lines changed

src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public virtual bool TryTest(
147147
}
148148

149149
// As per JsonPatch spec, the target location must exist for test to be successful
150-
if (!dictionary.ContainsKey(convertedKey))
150+
if (!dictionary.TryGetValue(convertedKey, out var currentValue))
151151
{
152152
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment);
153153
return false;
@@ -158,8 +158,6 @@ public virtual bool TryTest(
158158
return false;
159159
}
160160

161-
var currentValue = dictionary[convertedKey];
162-
163161
// The target segment does not have an assigned value to compare the test value with
164162
if (currentValue == null)
165163
{

src/Http/Authentication.Core/src/AuthenticationSchemeProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,14 @@ public virtual void AddScheme(AuthenticationScheme scheme)
184184
/// <param name="name">The name of the authenticationScheme being removed.</param>
185185
public virtual void RemoveScheme(string name)
186186
{
187-
if (!_schemes.ContainsKey(name))
187+
if (!_schemes.TryGetValue(name, out var scheme))
188188
{
189189
return;
190190
}
191191
lock (_lock)
192192
{
193-
if (_schemes.ContainsKey(name))
193+
if (_schemes.TryGetValue(name, out scheme))
194194
{
195-
var scheme = _schemes[name];
196195
if (_requestHandlers.Remove(scheme))
197196
{
198197
_requestHandlersCopy = _requestHandlers.ToArray();

src/Identity/Core/src/SignInManager.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -652,26 +652,21 @@ public virtual async Task<IEnumerable<AuthenticationScheme>> GetExternalAuthenti
652652
{
653653
var auth = await Context.AuthenticateAsync(IdentityConstants.ExternalScheme);
654654
var items = auth?.Properties?.Items;
655-
if (auth?.Principal == null || items == null || !items.ContainsKey(LoginProviderKey))
655+
if (auth?.Principal == null || items == null || !items.TryGetValue(LoginProviderKey, out var provider))
656656
{
657657
return null;
658658
}
659659

660660
if (expectedXsrf != null)
661661
{
662-
if (!items.ContainsKey(XsrfKey))
663-
{
664-
return null;
665-
}
666-
var userId = items[XsrfKey] as string;
667-
if (userId != expectedXsrf)
662+
if (!items.TryGetValue(XsrfKey, out var userId) ||
663+
userId != expectedXsrf)
668664
{
669665
return null;
670666
}
671667
}
672668

673669
var providerKey = auth.Principal.FindFirstValue(ClaimTypes.NameIdentifier);
674-
var provider = items[LoginProviderKey] as string;
675670
if (providerKey == null || provider == null)
676671
{
677672
return null;

0 commit comments

Comments
 (0)