// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System.Collections.Generic; namespace UltimateXR.Extensions.System.Collections { /// /// and extensions. /// public static class DictionaryExt { #region Public Methods /// /// Adds all elements in another dictionary to the dictionary. /// /// Key type /// Value type /// Destination dictionary /// Source dictionary /// Determines if duplicated keys must be overriden with other's values public static void AddRange(this IDictionary self, IDictionary other, bool overrideExistingKeys = false) { foreach (var kv in other) { if (!self.ContainsKey(kv.Key)) { self.Add(kv.Key, kv.Value); } else if (overrideExistingKeys) { self[kv.Key] = kv.Value; } } } /// /// Gets a given value defined by a key in a dictionary. If the key is not found, it is added and the value is given /// the default value. /// /// Dictionary /// Key to look for /// Key type /// Value type /// /// Value in the dictionary. If the key doesn't exist it will be added and the return value will be the default /// value /// public static TValue GetOrAddValue(this IDictionary self, TKey key) where TValue : new() { if (!self.TryGetValue(key, out TValue value)) { value = new TValue(); self.Add(key, value); } return value; } #endregion } }