private void Insert(int index, string key, MenuItem value) { if (count == keys.Length) EnsureCapacity(count + 1); if (index < count) { Array.Copy(keys, index, keys, index + 1, count - index); Array.Copy(values, index, values, index + 1, count - index); } keys[index] = key; values[index] = value; count++; version++; }
/// <summary> /// Removes the element at the specified index of the <b>MenuItemCollection</b>. /// </summary> /// <param name="index">The zero-based index of the element to remove.</param> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> is outside the range of valid indices for the <see cref="MenuItemCollection"/>. /// </exception> /// <exception cref="NotSupportedException"> /// <para>The <see cref="MenuItemCollection"/> is read-only.</para> /// <para>-or-</para> /// <para>The <b>MenuItemCollection</b> has a fixed size.</para> /// </exception> /// <remarks> /// <para>The index sequence is based on the sort sequence. When an element is added, /// it is inserted into <see cref="MenuItemCollection"/> in the correct sort order, and /// the indexing adjusts accordingly. When an element removed, the indexing also adjusts /// accordingly. Therefore, the index of a specific key-and-value pair might change as /// elements are added or removed from the <see cref="MenuItemCollection"/>.</para> /// <para>In collections of contiguous elements, such as lists, the elements that /// follow the removed element move up to occupy the vacated spot. If the collection is /// indexed, the indices of the elements that are moved are also updated.</para> /// </remarks> public virtual void RemoveAt(int index) { if (index < 0 || index >= count) throw new ArgumentOutOfRangeException("index", index, "The index is outside the range of valid indices."); count--; if (index < count) { Array.Copy(keys, index + 1, keys, index, count - index); Array.Copy(values, index + 1, values, index, count - index); } // We can't set the deleted entries equal to null, because they might be value types. // Instead, we'll create empty single-element arrays of the right type and copy them // over the entries we want to erase. string[] tempKey = new string[1]; MenuItem[] tempVal = new MenuItem[1]; Array.Copy(tempKey, 0, keys, count, 1); Array.Copy(tempVal, 0, values, count, 1); version++; }
/// <summary> /// Replaces the value at a specific index in the <b>MenuItemCollection</b>. /// </summary> /// <param name="index">The zero-based index at which to save <paramref name="value"/>.</param> /// <param name="value">The <see cref="MenuItem"/> to save into the <see cref="MenuItemCollection"/>.</param> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="index"/> is outside the range of valid indices for the <see cref="MenuItemCollection"/>. /// </exception> /// <remarks> /// <para>The index sequence is based on the sort sequence. When an element is added, /// it is inserted into <see cref="MenuItemCollection"/> in the correct sort order, and /// the indexing adjusts accordingly. When an element removed, the indexing also adjusts /// accordingly. Therefore, the index of a specific key-and-value pair might change as /// elements are added or removed from the <see cref="MenuItemCollection"/>.</para> /// </remarks> public virtual void SetByIndex(int index, MenuItem value) { if (index < 0 || index >= count) throw new ArgumentOutOfRangeException("index", index, "The index is outside the range of valid indices."); values[index] = value; version++; }
/// <summary> /// Determines whether the <b>MenuItemCollection</b> contains a specific value. /// </summary> /// <param name="value">The value to locate in the <see cref="MenuItemCollection"/>.</param> /// <returns> /// <b>true</b> if the <see cref="MenuItemCollection"/> contains an element with the specified /// <paramref name="value"/>; otherwise, <b>false</b>. /// </returns> public virtual bool ContainsValue(MenuItem value) { return (IndexOfValue(value) >= 0); }
/// <summary> /// Returns the zero-based index of the first occurrence of the specified value in /// the <b>MenuItemCollection</b>. /// </summary> /// <param name="value">The value to locate in the <see cref="MenuItemCollection"/>.</param> /// <returns> /// The zero-based index of <paramref name="value"/>, if <paramref name="value"/> is found in /// the <see cref="MenuItemCollection"/>; otherwise, -1. /// </returns> /// <remarks> /// <para>The index sequence is based on the sort sequence. When an element is added, /// it is inserted into <see cref="MenuItemCollection"/> in the correct sort order, and /// the indexing adjusts accordingly. When an element removed, the indexing also adjusts /// accordingly. Therefore, the index of a specific key-and-value pair might change as /// elements are added or removed from the <see cref="MenuItemCollection"/>.</para> /// <para>The values of the elements of the <see cref="MenuItemCollection"/> are compared to the /// specified value using the Equals method.</para> /// <para>This method uses a linear search; therefore, the average execution time is /// proportional to <see cref="MenuItemCollection.Count"/>.</para> /// </remarks> public virtual int IndexOfValue(MenuItem value) { return Array.IndexOf(values, value, 0, count); }
public virtual int IndexOf(MenuItem value) { return list.IndexOfValue(value); }
/// <summary> /// Adds an element with the specified key and value to the <b>MenuItemCollection</b>. /// </summary> /// <param name="key">The key of the element to add.</param> /// <param name="value">The value of the element to add.</param> /// <exception cref="ArgumentNullException">The <paramref name="key"/> is a null reference.</exception> /// <exception cref="ArgumentException"> /// <para>An element with the specified <paramref name="key"/> already exists in the <see cref="MenuItemCollection"/>.</para> /// <para>-or-</para> /// <para>The <b>MenuItemCollection</b> is set to use the <see cref="IComparable"/> interface, /// and <paramref name="key"/> does not implement the <b>IComparable</b> interface.</para> /// </exception> /// <exception cref="InvalidOperationException">The comparer throws an exception.</exception> /// <exception cref="NotSupportedException"> /// <para>The <see cref="MenuItemCollection"/> is read-only.</para> /// <para>-or-</para> /// <para>The <b>MenuItemCollection</b> has a fixed size.</para> /// </exception> public virtual void Add(string key, MenuItem value) { if (Object.ReferenceEquals(key, null)) // avoids compiler error for null check on value type throw new ArgumentNullException("key", "The key cannot be null."); int index = Array.BinarySearch(keys, 0, count, key, comparer); if (index >= 0) throw new ArgumentException(String.Format("Item has already been added. Key being added: \"{0}\".", key)); Insert(~index, key, value); }
public override void SetByIndex(int index, MenuItem value) { lock (root) list.SetByIndex(index, value); }
public virtual bool Contains(MenuItem value) { return list.ContainsValue(value); }
public override int IndexOfValue(MenuItem value) { lock (root) return list.IndexOfValue(value); }
public override bool ContainsValue(MenuItem value) { lock (root) return list.ContainsValue(value); }
public override void Add(string key, MenuItem value) { lock (root) list.Add(key, value); }
public virtual void Reset() { if (version != list.version) throw new InvalidOperationException("The collection was modified - enumeration cannot continue."); // We can't set the entries equal to null, because they might be value types. // Instead, we'll create empty single-element arrays of the right type and copy them // over the entries we want to erase. string[] tempKey = new string[1]; MenuItem[] tempVal = new MenuItem[1]; key = tempKey[0]; value = tempVal[0]; currentValid = false; index = startIndex; }
public virtual bool MoveNext() { if (version != list.version) throw new InvalidOperationException("The collection was modified - enumeration cannot continue."); if (index < endIndex) { key = list.keys[index]; value = list.values[index]; index++; currentValid = true; return true; } // We can't set the entries equal to null, because they might be value types. // Instead, we'll create empty single-element arrays of the right type and copy them // over the entries we want to erase. string[] tempKey = new string[1]; MenuItem[] tempVal = new MenuItem[1]; key = tempKey[0]; value = tempVal[0]; currentValid = false; return false; }