Skip to content

Commit

Permalink
Use TryGetValue() for dictionary (dotnet/extensions#1209)
Browse files Browse the repository at this point in the history
Use TryGetValue() when accessing the mapping dictionary, rather than ContainsKey()+indexer.


Commit migrated from dotnet/extensions@d6900b4
  • Loading branch information
martincostello authored and Tratcher committed Mar 2, 2019
1 parent 15ae448 commit fb9befb
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public override void Load()
}

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.ContainsKey(currentArg))
if (_switchMappings != null && _switchMappings.TryGetValue(currentArg, out var mappedKey))
{
key = _switchMappings[currentArg];
key = mappedKey;
}
// If the switch starts with a single "-" and it isn't in given mappings , it is an invalid usage so ignore it
else if (keyStartIndex == 1)
Expand All @@ -105,9 +105,9 @@ public override void Load()
var keySegment = currentArg.Substring(0, separator);

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.ContainsKey(keySegment))
if (_switchMappings != null && _switchMappings.TryGetValue(keySegment, out var mappedKeySegment))
{
key = _switchMappings[keySegment];
key = mappedKeySegment;
}
// If the switch starts with a single "-" and it isn't in given mappings , it is an invalid usage
else if (keyStartIndex == 1)
Expand Down

0 comments on commit fb9befb

Please sign in to comment.