/// <summary> /// Initializes a new instance of the <see cref="Bag<T>" /> class. /// </summary> /// <param name="dictionary"> The dictionary to copy values from. </param> Bag(IDictionary <T, int> dictionary) { #region Asserts Debug.Assert(dictionary != null); #endregion _data = new VisitableHashtable <T, int>(dictionary); // Update the count using (var enumerator = _data.GetEnumerator()) while (enumerator.MoveNext()) { Count += enumerator.Current.Value; } }
/// <summary> /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see /// cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index. /// </summary> /// <param name="array"> The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see /// cref="T:System.Collections.Generic.ICollection`1"></see> . The <see cref="T:System.Array"></see> must have zero-based indexing. </param> /// <param name="arrayIndex"> The zero-based index in array at which copying begins. </param> /// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception> /// <exception cref="T:System.ArgumentNullException">array is null.</exception> /// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source /// <see cref="T:System.Collections.Generic.ICollection`1"></see> /// is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception> public void CopyTo(T[] array, int arrayIndex) { if (array == null) { throw new ArgumentNullException("array"); } if ((array.Length - arrayIndex) < Count) { throw new ArgumentException(Resources.NotEnoughSpaceInTargetArray); } var enumerator = _data.GetEnumerator(); var counter = arrayIndex; while (enumerator.MoveNext()) { var itemCount = enumerator.Current.Value; var obj = enumerator.Current.Key; for (var i = 0; i < itemCount; i++) { array.SetValue(obj, counter++); } } enumerator.Dispose(); }