From fb9befb67e4df01c2bd710ad7f0559926d7b2aa3 Mon Sep 17 00:00:00 2001 From: Martin Costello Date: Sat, 2 Mar 2019 15:17:31 +0000 Subject: [PATCH] Use TryGetValue() for dictionary (dotnet/Extensions#1209) Use TryGetValue() when accessing the mapping dictionary, rather than ContainsKey()+indexer. Commit migrated from https://github.com/dotnet/Extensions/commit/d6900b45f50a905e66374b96d7e5af558b0bf910 --- .../src/CommandLineConfigurationProvider.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.CommandLine/src/CommandLineConfigurationProvider.cs b/src/libraries/Microsoft.Extensions.Configuration.CommandLine/src/CommandLineConfigurationProvider.cs index 937ffbfd60814..945d74f4d0da2 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.CommandLine/src/CommandLineConfigurationProvider.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.CommandLine/src/CommandLineConfigurationProvider.cs @@ -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) @@ -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)