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.ContainsKey("dataversion"))
if (!jsonDictionary.TryGetValue("dataversion", out object? value))
{
jsonDictionary.Add("dataversion", dataVersion);
value = dataVersion;
jsonDictionary.Add("dataversion", value);
}
if (!jsonDictionary.ContainsKey("variantassignment"))
@ -87,7 +88,7 @@ namespace AllExperiments
}
else
{
var jsonDataVersion = jsonDictionary["dataversion"].ToString();
var jsonDataVersion = value.ToString();
if (jsonDataVersion != null && int.Parse(jsonDataVersion, CultureInfo.InvariantCulture) < dataVersion)
{
jsonDictionary["dataversion"] = dataVersion;
@ -116,9 +117,9 @@ namespace AllExperiments
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;
}

View File

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

View File

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

View File

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

View File

@ -201,12 +201,13 @@ namespace Microsoft.PowerToys.Run.Plugin.WindowsTerminal
{
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 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.
_protectedProcessVariables.Add(pVarKey);

View File

@ -248,14 +248,14 @@ namespace PowerLauncher
var defaultPlugins = GetDefaultPluginsSettings().ToDictionary(x => x.Id);
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);
plugin.Name = defaultPlugins[plugin.Id].Name;
plugin.Description = defaultPlugins[plugin.Id].Description;
plugin.Author = defaultPlugins[plugin.Id].Author;
plugin.IconPathDark = defaultPlugins[plugin.Id].IconPathDark;
plugin.IconPathLight = defaultPlugins[plugin.Id].IconPathLight;
var additionalOptions = CombineAdditionalOptions(value.AdditionalOptions, plugin.AdditionalOptions);
plugin.Name = value.Name;
plugin.Description = value.Description;
plugin.Author = value.Author;
plugin.IconPathDark = value.IconPathDark;
plugin.IconPathLight = value.IconPathLight;
defaultPlugins[plugin.Id] = plugin;
defaultPlugins[plugin.Id].AdditionalOptions = additionalOptions;
}

View File

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