private void AddKey(TKey key, TItem item) { if (dict != null) { dict.Add(key, item); } else if (keyCount == threshold) { CreateDictionary(); dict.Add(key, item); } else { if (Contains(key)) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate); } keyCount++; } }
protected void ChangeItemKey(TItem item, TKey newKey) { if (!this.ContainsItem(item)) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_ItemNotExist); } TKey keyForItem = this.GetKeyForItem(item); if (this.comparer.Equals(keyForItem, newKey)) { return; } if ((object)newKey != null) { this.AddKey(newKey, item); } if ((object)keyForItem == null) { return; } this.RemoveKey(keyForItem); }
protected void ChangeItemKey(TItem item, TKey newKey) { // check if the item exists in the collection if (!ContainsItem(item)) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_ItemNotExist); } TKey oldKey = GetKeyForItem(item); if (!comparer.Equals(oldKey, newKey)) { if (newKey != null) { AddKey(newKey, item); } if (oldKey != null) { RemoveKey(oldKey); } } }
public void RemoveRange(int index, int count) { if (items.IsReadOnly) { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); } if ((uint)index > (uint)items.Count) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } if (count < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (index > items.Count - count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); } RemoveItemsRange(index, count); }
void ICollection.CopyTo(Array array, int index) { if (array == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (array.Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported); } if (array.GetLowerBound(0) != 0) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound); } if (index < 0) { ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < Count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } if (array is T[] tArray) { items.CopyTo(tArray, index); } else { // // Catch the obvious case assignment will fail. // We can't find all possible problems by doing the check though. // For example, if the element type of the Array is derived from T, // we can't figure out if we can successfully copy the element beforehand. // Type targetType = array.GetType().GetElementType(); Type sourceType = typeof(T); if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType))) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } // // We can't cast array of value type to object[], so we don't support // widening of primitive types here. // object[] objects = array as object[]; if (objects == null) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } int count = items.Count; try { for (int i = 0; i < count; i++) { objects[index++] = items[i]; } } catch (ArrayTypeMismatchException) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } } }
// Abstracted away to avoid redundant implementations. internal static void CopyToNonGenericICollectionHelper <T>(ICollection <T> collection, Array array, int index) { if (array == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (array.Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported); } if (array.GetLowerBound(0) != 0) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound); } if (index < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (array.Length - index < collection.Count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } // Easy out if the ICollection<T> implements the non-generic ICollection ICollection nonGenericCollection = collection as ICollection; if (nonGenericCollection != null) { nonGenericCollection.CopyTo(array, index); return; } T[] items = array as T[]; if (items != null) { collection.CopyTo(items, index); } else { // // Catch the obvious case assignment will fail. // We can found all possible problems by doing the check though. // For example, if the element type of the Array is derived from T, // we can't figure out if we can successfully copy the element beforehand. // Type targetType = array.GetType().GetElementType(); Type sourceType = typeof(T); if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType))) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } // // We can't cast array of value type to object[], so we don't support // widening of primitive types here. // object[] objects = array as object[]; if (objects == null) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } try { foreach (var item in collection) { objects[index++] = item; } } catch (ArrayTypeMismatchException) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } } }
void ICollection.CopyTo(Array array, int index) { if (array == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (array.Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported); } if (array.GetLowerBound(0) != 0) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound); } if (index < 0 || index > array.Length) { ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < Count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } KeyValuePair <TKey, TValue>[] pairs = array as KeyValuePair <TKey, TValue>[]; if (pairs != null) { m_dictionary.CopyTo(pairs, index); } else { DictionaryEntry[] dictEntryArray = array as DictionaryEntry[]; if (dictEntryArray != null) { foreach (var item in m_dictionary) { dictEntryArray[index++] = new DictionaryEntry(item.Key, item.Value); } } else { object[] objects = array as object[]; if (objects == null) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } try { foreach (var item in m_dictionary) { objects[index++] = new KeyValuePair <TKey, TValue>(item.Key, item.Value); } } catch (ArrayTypeMismatchException) { ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } } } }
internal static void CopyToNonGenericICollectionHelper <T>(ICollection <T> collection, Array array, int index) { if (array == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } if (array.Rank != 1) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported); } if (array.GetLowerBound(0) != 0) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound); } if (index < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); } if (array.Length - index < collection.Count) { ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } ICollection collection1 = collection as ICollection; if (collection1 != null) { collection1.CopyTo(array, index); } else { T[] array1 = array as T[]; if (array1 != null) { collection.CopyTo(array1, index); } else { Type elementType = array.GetType().GetElementType(); Type c = typeof(T); if (!elementType.IsAssignableFrom(c) && !c.IsAssignableFrom(elementType)) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); } object[] objArray = array as object[]; if (objArray == null) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); } try { foreach (T obj in (IEnumerable <T>)collection) { objArray[index++] = (object)obj; } } catch (ArrayTypeMismatchException ex) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); } } } }