diff --git a/ICD.Common.Utils/Extensions/DictionaryExtensions.cs b/ICD.Common.Utils/Extensions/DictionaryExtensions.cs index d21049e..d362a3a 100644 --- a/ICD.Common.Utils/Extensions/DictionaryExtensions.cs +++ b/ICD.Common.Utils/Extensions/DictionaryExtensions.cs @@ -131,15 +131,30 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - try - { - return extends.GetKeys(value).First(); - } - catch (InvalidOperationException) - { - string message = string.Format("Unable to find Key with Value matching {0}", value); - throw new KeyNotFoundException(message); - } + TKey output; + if (extends.TryGetKey(value, out output)) + return output; + + string message = string.Format("Unable to find Key with Value matching {0}", value); + throw new KeyNotFoundException(message); + } + + /// + /// Attempts to get the first key with the given value. + /// + /// + /// + /// + /// + /// + /// + [PublicAPI] + public static bool TryGetKey(this IDictionary extends, TValue value, out TKey key) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return extends.GetKeys(value).TryElementAt(0, out key); } ///