/// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <param name="key">The key of the value to get or set.</param> /// <exception cref="KeyNotFoundException">The property is retrieved and key does not exist in the collection.</exception> /// <returns>The value associated with the specified key. If the specified key is not found, /// a get operation throws a <see cref="KeyNotFoundException"/>, and a set operation creates a new element with the specified key.</returns> public TValue this[TKey key] { get { ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); TValue value; if (TryGetValue(key, out value)) { return(value); } throw new KeyNotFoundException(); } set { ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); // value == null check here is redundant, but avoids boxing. if (value == null) { ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value)); } LinkedListNode <KeyValuePair <TKey, TValue> > node; var pair = new KeyValuePair <TKey, TValue>(key, value); if (map.TryGetValue(key, out node)) { node.Value = pair; } else { node = list.AddLast(pair); map[key] = node; } } }
/// <summary> /// Inserts the given item at the specified index. /// </summary> /// <param name="index">The index at which to insert the item.</param> /// <param name="item">The item to insert.</param> public void Insert(int index, T item) { ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); if (index < 0 || index > count) { throw new ArgumentOutOfRangeException(nameof(index)); } EnsureSize(count + 1); Array.Copy(array, index, array, index + 1, count - index); array[index] = item; count++; }
/// <summary> /// Returns the index of the given item within the collection, or -1 if the item is not /// present. /// </summary> /// <param name="item">The item to find in the collection.</param> /// <returns>The zero-based index of the item, or -1 if it is not found.</returns> public int IndexOf(T item) { ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); EqualityComparer <T> comparer = EqualityComparer <T> .Default; for (int i = 0; i < count; i++) { if (comparer.Equals(array[i], item)) { return(i); } } return(-1); }
/// <summary> /// Gets or sets the item at the specified index. /// </summary> /// <value> /// The element at the specified index. /// </value> /// <param name="index">The zero-based index of the element to get or set.</param> /// <returns>The item at the specified index.</returns> public new T this[int index] { get { return(base[index]); } set { int count = Count; if (index < 0 || index >= count) { throw new ArgumentOutOfRangeException("index"); } ProtoPreconditions.CheckNotNullUnconstrained(value, "value"); base[index] = value; } }
/// <summary> /// Removes the entry identified by the given key from the map. /// </summary> /// <param name="key">The key indicating the entry to remove from the map.</param> /// <returns><c>true</c> if the map contained the given key before the entry was removed; <c>false</c> otherwise.</returns> public bool Remove(TKey key) { ProtoPreconditions.CheckNotNullUnconstrained(key, "key"); if (map.TryGetValue(key, out LinkedListNode <KeyValuePair <TKey, TValue> > node)) { map.Remove(key); node.List.Remove(node); return(true); } else { return(false); } }
/// <summary> /// Gets or sets the item at the specified index. /// </summary> /// <value> /// The element at the specified index. /// </value> /// <param name="index">The zero-based index of the element to get or set.</param> /// <returns>The item at the specified index.</returns> public T this[int index] { get { if (index < 0 || index >= count) { throw new ArgumentOutOfRangeException(nameof(index)); } return(array[index]); } set { if (index < 0 || index >= count) { throw new ArgumentOutOfRangeException(nameof(index)); } ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value)); array[index] = value; } }
/// <summary> /// Adds the specified item to the collection. /// </summary> /// <param name="item">The item to add.</param> public void Add(T item) { ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item)); EnsureSize(count + 1); array[count++] = item; }
/// <summary> /// Determines whether the specified key is present in the map. /// </summary> /// <param name="key">The key to check.</param> /// <returns><c>true</c> if the map contains the given key; <c>false</c> otherwise.</returns> public bool ContainsKey(TKey key) { ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key)); return(map.ContainsKey(key)); }
/// <summary> /// Inserts the given item at the specified index. /// </summary> /// <param name="index">The index at which to insert the item.</param> /// <param name="item">The item to insert.</param> public new void Insert(int index, T item) { ProtoPreconditions.CheckNotNullUnconstrained(item, "item"); base.Insert(index, item); }
/// <summary> /// Returns the index of the given item within the collection, or -1 if the item is not /// present. /// </summary> /// <param name="item">The item to find in the collection.</param> /// <returns>The zero-based index of the item, or -1 if it is not found.</returns> public new int IndexOf(T item) { ProtoPreconditions.CheckNotNullUnconstrained(item, "item"); return(base.IndexOf(item)); }
/// <summary> /// Adds the specified item to the collection. /// </summary> /// <param name="item">The item to add.</param> public new void Add(T item) { ProtoPreconditions.CheckNotNullUnconstrained(item, "item"); base.Add(item); }