diff --git a/FluentFTP/Helpers/Hashing/HashAlgorithms.cs b/FluentFTP/Helpers/Hashing/HashAlgorithms.cs index f2f5e48b5..07282daa0 100644 --- a/FluentFTP/Helpers/Hashing/HashAlgorithms.cs +++ b/FluentFTP/Helpers/Hashing/HashAlgorithms.cs @@ -29,11 +29,9 @@ internal static class HashAlgorithms { /// Name of the hash algorithm /// The FtpHashAlgorithm public static FtpHashAlgorithm FromString(string name) { - if (!NameToEnum.ContainsKey(name.ToUpper())) { - throw new NotImplementedException("Unknown hash algorithm: " + name); - } - - return NameToEnum[name]; + return NameToEnum.TryGetValue(name, out var value) + ? value + : throw new NotImplementedException("Unknown hash algorithm: " + name); } /// @@ -42,11 +40,9 @@ public static FtpHashAlgorithm FromString(string name) { /// FtpHashAlgorithm to be converted into string /// Name of the hash algorithm public static string PrintToString(this FtpHashAlgorithm name) { - if (!EnumToName.ContainsKey(name)) { - return name.ToString(); - } - - return EnumToName[name]; + return EnumToName.TryGetValue(name, out var value) + ? value + : name.ToString(); } private static readonly List AlgoPreference = new List { diff --git a/FluentFTP/Streams/FtpSocketStream.cs b/FluentFTP/Streams/FtpSocketStream.cs index 43540e9de..a5fc71abe 100644 --- a/FluentFTP/Streams/FtpSocketStream.cs +++ b/FluentFTP/Streams/FtpSocketStream.cs @@ -862,18 +862,14 @@ private static bool IsIpVersionAllowed(IPAddress ipad, FtpIpVersion ipVersions, /// /// The host to query private IPAddress[] GetCachedHostAddresses(string host) { - IPAddress[] ipads; - if (Client.Status.CachedHostIpads.ContainsKey(host)) { - ipads = Client.Status.CachedHostIpads[host]; - } - else { + if (!Client.Status.CachedHostIpads.TryGetValue(host, out IPAddress[] ipads)) { #if NETSTANDARD || NET5_0_OR_GREATER ipads = Dns.GetHostAddressesAsync(host).Result; #else ipads = Dns.GetHostAddresses(host); #endif - Client.Status.CachedHostIpads.Add(host, ipads); + Client.Status.CachedHostIpads[host] = ipads; } return ipads; @@ -885,12 +881,7 @@ private IPAddress[] GetCachedHostAddresses(string host) { /// The host to query /// The IP address to store in the cache private void SetCachedHostAddresses(string host, IPAddress ipad) { - if (Client.Status.CachedHostIpads.ContainsKey(host)) { - Client.Status.CachedHostIpads[host] = new IPAddress[1] { ipad }; - } - else { - Client.Status.CachedHostIpads.Add(host, new IPAddress[1] { ipad }); - } + Client.Status.CachedHostIpads[host] = new IPAddress[1] { ipad }; } /// @@ -1021,18 +1012,14 @@ private bool ConnectHelper(IPAddress ipad, int port) { /// The host to query /// The token that can be used to cancel the entire process private async Task GetCachedHostAddressesAsync(string host, CancellationToken token) { - IPAddress[] ipads; - if (Client.Status.CachedHostIpads.ContainsKey(host)) { - ipads = Client.Status.CachedHostIpads[host]; - } - else { + if (!Client.Status.CachedHostIpads.TryGetValue(host, out IPAddress[] ipads)) { #if NET6_0_OR_GREATER ipads = await Dns.GetHostAddressesAsync(host, token); #else ipads = await Dns.GetHostAddressesAsync(host); #endif - Client.Status.CachedHostIpads.Add(host, ipads); + Client.Status.CachedHostIpads[host] = ipads; } return ipads;