コード例 #1
0
 /// <summary>
 /// Atomically adds a new item to the map.
 /// If the key already exists, then the new item is ignored
 /// </summary>
 /// <remarks>Null is not allowed for a Key or a Value</remarks>
 /// <param name="key">Key</param>
 /// <param name="value">Value</param>
 /// <exception cref="ArgumentNullException">Throws ArgumentNullException the key or value are null</exception>
 /// <returns>New Map with the item added</returns>
 public Map <K, V> TryAdd(K key, V value)
 {
     if (isnull(key))
     {
         throw new ArgumentNullException(nameof(key));
     }
     return(SetRoot(MapModule.TryAdd(Root, key, value, Comparer <K> .Default)));
 }
コード例 #2
0
        /// <summary>
        /// Atomically adds a range of items to the map.  If any of the keys exist already
        /// then they're ignored.
        /// </summary>
        /// <remarks>Null is not allowed for a Key or a Value</remarks>
        /// <param name="range">Range of KeyValuePairs to add</param>
        /// <exception cref="ArgumentNullException">Throws ArgumentNullException the keys or values are null</exception>
        /// <returns>New Map with the items added</returns>
        public Map <K, V> TryAddRange(IEnumerable <KeyValuePair <K, V> > range)
        {
            if (range == null)
            {
                return(this);
            }

            var self = Root;

            foreach (var item in range)
            {
                if (isnull(item.Key))
                {
                    throw new ArgumentNullException(nameof(item.Key));
                }
                self = MapModule.TryAdd(self, item.Key, item.Value, Comparer <K> .Default);
            }
            return(SetRoot(self));
        }