CA1854: Use TryGetValue instead of ContainsKey

This commit is contained in:
Jeremy Sinclair 2023-09-13 19:44:25 -04:00
parent 0c8d78ea5a
commit 40a9b51ebb
8 changed files with 28 additions and 26 deletions

View File

@ -76,9 +76,10 @@ namespace AllExperiments
if (jsonDictionary != null) if (jsonDictionary != null)
{ {
if (!jsonDictionary.ContainsKey("dataversion")) if (!jsonDictionary.TryGetValue("dataversion", out object? value))
{ {
jsonDictionary.Add("dataversion", dataVersion); value = dataVersion;
jsonDictionary.Add("dataversion", value);
} }
if (!jsonDictionary.ContainsKey("variantassignment")) if (!jsonDictionary.ContainsKey("variantassignment"))
@ -87,7 +88,7 @@ namespace AllExperiments
} }
else else
{ {
var jsonDataVersion = jsonDictionary["dataversion"].ToString(); var jsonDataVersion = value.ToString();
if (jsonDataVersion != null && int.Parse(jsonDataVersion, CultureInfo.InvariantCulture) < dataVersion) if (jsonDataVersion != null && int.Parse(jsonDataVersion, CultureInfo.InvariantCulture) < dataVersion)
{ {
jsonDictionary["dataversion"] = dataVersion; jsonDictionary["dataversion"] = dataVersion;
@ -116,9 +117,9 @@ namespace AllExperiments
if (jsonDictionary != null) if (jsonDictionary != null)
{ {
if (jsonDictionary.ContainsKey("variantassignment")) if (jsonDictionary.TryGetValue("variantassignment", out object? value))
{ {
if (jsonDictionary["variantassignment"].ToString() == "alternate" && AssignmentUnit != string.Empty) if (value.ToString() == "alternate" && AssignmentUnit != string.Empty)
{ {
IsExperiment = true; IsExperiment = true;
} }

View File

@ -328,13 +328,13 @@ namespace ManagedCommon
char paramFormat; char paramFormat;
string paramType = formatString.Substring(formatterPosition + 1, 2); string paramType = formatString.Substring(formatterPosition + 1, 2);
int paramCount = 3; int paramCount = 3;
if (DefaultFormatTypes.ContainsKey(paramType)) if (DefaultFormatTypes.TryGetValue(paramType, out char value))
{ {
// check the next char, which could be a formatter // check the next char, which could be a formatter
if (formatterPosition >= formatString.Length - 3) if (formatterPosition >= formatString.Length - 3)
{ {
// not enough characters, end of string, no formatter, use the default one // not enough characters, end of string, no formatter, use the default one
paramFormat = DefaultFormatTypes[paramType]; paramFormat = value;
paramCount = 2; paramCount = 2;
} }
else else
@ -344,7 +344,7 @@ namespace ManagedCommon
// check if it a valid formatter // check if it a valid formatter
if (!FormatTypeToStringFormatters.ContainsKey(paramFormat)) if (!FormatTypeToStringFormatters.ContainsKey(paramFormat))
{ {
paramFormat = DefaultFormatTypes[paramType]; paramFormat = value;
paramCount = 2; paramCount = 2;
} }
} }

View File

@ -115,7 +115,7 @@ namespace MouseWithoutBorders
byte[] rv; byte[] rv;
string myKey = Common.MyKey; string myKey = Common.MyKey;
if (!LegalKeyDictionary.ContainsKey(myKey)) if (!LegalKeyDictionary.TryGetValue(myKey, out byte[] value))
{ {
Rfc2898DeriveBytes key = new( Rfc2898DeriveBytes key = new(
myKey, myKey,
@ -127,7 +127,7 @@ namespace MouseWithoutBorders
} }
else else
{ {
rv = LegalKeyDictionary[myKey]; rv = value;
} }
return rv; return rv;

View File

@ -43,12 +43,12 @@ namespace FancyZonesEditor.Models
public bool SelectKey(string key, string uuid) public bool SelectKey(string key, string uuid)
{ {
if (!SelectedKeys.ContainsKey(key)) if (!SelectedKeys.TryGetValue(key, out string value))
{ {
return false; return false;
} }
if (SelectedKeys[key] == uuid) if (value == uuid)
{ {
return true; return true;
} }

View File

@ -201,12 +201,13 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsTerminal
{ {
var aumid = terminal.AppUserModelId; var aumid = terminal.AppUserModelId;
if (!_logoCache.ContainsKey(aumid)) if (!_logoCache.TryGetValue(aumid, out BitmapImage value))
{ {
_logoCache.Add(aumid, terminal.GetLogo()); value = terminal.GetLogo();
_logoCache.Add(aumid, value);
} }
return _logoCache[aumid]; return value;
} }
} }
} }

View File

@ -50,9 +50,9 @@ namespace PowerLauncher.Helper
string pVarKey = (string)pVar.Key; string pVarKey = (string)pVar.Key;
string pVarValue = (string)pVar.Value; string pVarValue = (string)pVar.Value;
if (machineAndUserVars.ContainsKey(pVarKey)) if (machineAndUserVars.TryGetValue(pVarKey, out string value))
{ {
if (machineAndUserVars[pVarKey] != pVarValue) if (value != pVarValue)
{ {
// Variable value for this process differs form merged machine/user value. // Variable value for this process differs form merged machine/user value.
_protectedProcessVariables.Add(pVarKey); _protectedProcessVariables.Add(pVarKey);

View File

@ -248,14 +248,14 @@ namespace PowerLauncher
var defaultPlugins = GetDefaultPluginsSettings().ToDictionary(x => x.Id); var defaultPlugins = GetDefaultPluginsSettings().ToDictionary(x => x.Id);
foreach (PowerLauncherPluginSettings plugin in settings.Plugins) foreach (PowerLauncherPluginSettings plugin in settings.Plugins)
{ {
if (defaultPlugins.ContainsKey(plugin.Id)) if (defaultPlugins.TryGetValue(plugin.Id, out PowerLauncherPluginSettings value))
{ {
var additionalOptions = CombineAdditionalOptions(defaultPlugins[plugin.Id].AdditionalOptions, plugin.AdditionalOptions); var additionalOptions = CombineAdditionalOptions(value.AdditionalOptions, plugin.AdditionalOptions);
plugin.Name = defaultPlugins[plugin.Id].Name; plugin.Name = value.Name;
plugin.Description = defaultPlugins[plugin.Id].Description; plugin.Description = value.Description;
plugin.Author = defaultPlugins[plugin.Id].Author; plugin.Author = value.Author;
plugin.IconPathDark = defaultPlugins[plugin.Id].IconPathDark; plugin.IconPathDark = value.IconPathDark;
plugin.IconPathLight = defaultPlugins[plugin.Id].IconPathLight; plugin.IconPathLight = value.IconPathLight;
defaultPlugins[plugin.Id] = plugin; defaultPlugins[plugin.Id] = plugin;
defaultPlugins[plugin.Id].AdditionalOptions = additionalOptions; defaultPlugins[plugin.Id].AdditionalOptions = additionalOptions;
} }

View File

@ -134,7 +134,7 @@ public class Alphabet : IAlphabet
return _empty2DStringArray; return _empty2DStringArray;
} }
if (!_pinyinCache.ContainsKey(characters)) if (!_pinyinCache.TryGetValue(characters, out string[][] value))
{ {
var allPinyins = new List<string[]>(); var allPinyins = new List<string[]>();
foreach (var c in characters) foreach (var c in characters)
@ -158,7 +158,7 @@ public class Alphabet : IAlphabet
} }
else else
{ {
return _pinyinCache[characters]; return value;
} }
} }