IOrderedDictionary.Insert Method (Int32, Object, Object)
Inserts a key/value pair into the collection at the specified index.
Assembly: System (in System.dll)
Parameters
- index
-
Type:
System.Int32
The zero-based index at which the key/value pair should be inserted.
- key
-
Type:
System.Object
The object to use as the key of the element to add.
- value
-
Type:
System.Object
The object to use as the value of the element to add. The value can be null.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | |
| ArgumentNullException | key is null. |
| ArgumentException | An element with the same key already exists in the IOrderedDictionary collection. |
| NotSupportedException | The IOrderedDictionary collection is read-only. -or- The IOrderedDictionary collection has a fixed size. |
IOrderedDictionary accepts null as a valid value and allows duplicate elements.
If the index parameter is equal to Count, the value parameter is added to the end of the IOrderedDictionary collection.
In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accommodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped together, such as a hash table.
The following code example demonstrates the implementation of a simple IOrderedDictionary based on the ArrayList class. The implemented IOrderedDictionary stores first names as the keys and last names as the values, with the added requirement that each first name is unique. This code is part of a larger code example provided for the IOrderedDictionary class.
public class People : IOrderedDictionary { private ArrayList _people; public People(int numItems) { _people = new ArrayList(numItems); } public int IndexOfKey(object key) { for (int i = 0; i < _people.Count; i++) { if (((DictionaryEntry)_people[i]).Key == key) return i; } // key not found, reutrn -1. return -1; } public object this[object key] { get { return ((DictionaryEntry)_people[IndexOfKey(key)]).Value; } set { _people[IndexOfKey(key)] = new DictionaryEntry(key, value); } } // IOrderedDictionary Members public IDictionaryEnumerator GetEnumerator() { return new PeopleEnum(_people); } public void Insert(int index, object key, object value) { if (IndexOfKey(key) != -1) { throw new ArgumentException("An element with the same key already exists in the collection."); } _people.Insert(index, new DictionaryEntry(key, value)); } public void RemoveAt(int index) { _people.RemoveAt(index); } public object this[int index] { get { return ((DictionaryEntry)_people[index]).Value; } set { object key = ((DictionaryEntry)_people[index]).Key; _people[index] = new DictionaryEntry(Keys, value); } } // IDictionary Members public void Add(object key, object value) { if (IndexOfKey(key) != -1) { throw new ArgumentException("An element with the same key already exists in the collection."); } _people.Add(new DictionaryEntry(key, value)); } public void Clear() { _people.Clear(); } public bool Contains(object key) { if (IndexOfKey(key) == -1) { return false; } else { return true; } } public bool IsFixedSize { get { return false; } } public bool IsReadOnly { get { return false; } } public ICollection Keys { get { ArrayList KeyCollection = new ArrayList(_people.Count); for (int i = 0; i < _people.Count; i++) { KeyCollection.Add( ((DictionaryEntry)_people[i]).Key ); } return KeyCollection; } } public void Remove(object key) { _people.RemoveAt(IndexOfKey(key)); } public ICollection Values { get { ArrayList ValueCollection = new ArrayList(_people.Count); for (int i = 0; i < _people.Count; i++) { ValueCollection.Add( ((DictionaryEntry)_people[i]).Value ); } return ValueCollection; } } // ICollection Members public void CopyTo(Array array, int index) { _people.CopyTo(array, index); } public int Count { get { return _people.Count; } } public bool IsSynchronized { get { return _people.IsSynchronized; } } public object SyncRoot { get { return _people.SyncRoot; } } // IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { return new PeopleEnum(_people); } } public class PeopleEnum : IDictionaryEnumerator { public ArrayList _people; // Enumerators are positioned before the first element // until the first MoveNext() call. int position = -1; public PeopleEnum(ArrayList list) { _people = list; } public bool MoveNext() { position++; return (position < _people.Count); } public void Reset() { position = -1; } public object Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } public DictionaryEntry Entry { get { return (DictionaryEntry)Current; } } public object Key { get { try { return ((DictionaryEntry)_people[position]).Key; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } public object Value { get { try { return ((DictionaryEntry)_people[position]).Value; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } }
Available since 10
.NET Framework
Available since 2.0