///// <summary> ///// Returns the value associated with the specified key if there ///// already is one, or inserts the specified value and returns it. ///// </summary> ///// <typeparam name="TKey">Type of key</typeparam> ///// <typeparam name="TValue">Type of value</typeparam> ///// <param name="dictionary">Dictionary to access</param> ///// <param name="key">Key to lookup</param> ///// <param name="missingValue">Value to use when key is missing</param> ///// <returns>Existing value in the dictionary, or new one inserted</returns> //public TValue Key_GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, TValue missingValue) //{ // TValue result; // if (dictionary.TryGetValue(key, out result) == false) // { // result = missingValue; // dictionary[key] = result; // } // return result; //} /// <summary> /// Returns the value associated with the specified key if there /// already is one, or inserts the specified value and returns it. /// </summary> /// <typeparam name="TKey">Type of key</typeparam> /// <typeparam name="TValue">Type of value</typeparam> /// <param name="dictionary">Dictionary to access</param> /// <param name="key">Key to lookup</param> /// <param name="newValue">Value to use when key is missing</param> /// <param name="onError">The on error.</param> public void Key_AddSafe <TKey, TValue>(IDictionary <TKey, TValue> dictionary, TKey key, TValue newValue, enCompare_DuplicateError onError = enCompare_DuplicateError.Ignore) { TValue result; if (dictionary.TryGetValue(key, out result) == true) { switch (onError) { case enCompare_DuplicateError.Replace: dictionary[key] = newValue; return; // <============================ case enCompare_DuplicateError.Ignore: return; case enCompare_DuplicateError.Error: throw new ArgumentException("newValue"); default: throw new Exception($"Argument '{nameof(onError)}' error."); } } dictionary.Add(key, newValue); }
/// <summary>Convert list to dictionary.</summary> /// <param name="list">The list.</param> /// <param name="seperator">The seperator.</param> /// <param name="reverseOrder">if set to <c>true</c> [reverse order] of the keys and values.</param> /// <param name="onError">The on error.</param> /// <returns></returns> public IDictionary <string, string> ToDictionary(IList <string> list, string seperator = "=", bool reverseOrder = false, enCompare_DuplicateError onError = enCompare_DuplicateError.Ignore) { var result = new Dictionary <string, string>(list.Count); // Replace with longer version foreach (string abbr in list) { var id = abbr.zvar_Id(seperator); var value = abbr.zvar_Value(seperator); if (reverseOrder) { _lamed.Types.Dictionary.Key_AddSafe(result, value, id, onError); } else { _lamed.Types.Dictionary.Key_AddSafe(result, id, value); } } return(result); }
/// <summary> /// Returns the value associated with the specified key if there /// already is one, or inserts the specified value and returns it. /// </summary> /// <typeparam name="TKey">Type of key</typeparam> /// <typeparam name="TValue">Type of value</typeparam> /// <param name="dictionary">Dictionary to access</param> /// <param name="key">Key to lookup</param> /// <param name="newValue">Value to use when key is missing</param> /// <param name="onError">The on error.</param> public static void zDictionary_AddSafe <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key, TValue newValue, enCompare_DuplicateError onError = enCompare_DuplicateError.Ignore) { LamedalCore_.Instance.Types.Dictionary.Key_AddSafe(dictionary, key, newValue, onError); }