IOrderedDictionary::RemoveAt Method (Int32)
Removes the element at the specified index.
Assembly: System (in System.dll)
Parameters
- index
-
Type:
System::Int32
The zero-based index of the element to remove.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | |
| NotSupportedException | The IOrderedDictionary collection is read-only. -or- The IOrderedDictionary collection has a fixed size. |
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 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 ref class PeopleEnum : IDictionaryEnumerator { private: // Enumerators are positioned before the first element // until the first MoveNext() call. int position; ArrayList^ _people; public: PeopleEnum(ArrayList^ list) { this->Reset(); _people = list; } virtual bool MoveNext() { position++; return (position < _people->Count); } virtual void Reset() { position = -1; } virtual property Object^ Current { Object^ get() { try { return _people[position]; } catch (IndexOutOfRangeException^) { throw gcnew InvalidOperationException(); } } } virtual property DictionaryEntry Entry { DictionaryEntry get() { return (DictionaryEntry)(Current); } } virtual property Object^ Key { Object^ get() { try { return ((DictionaryEntry^)_people[position])->Key; } catch (IndexOutOfRangeException^) { throw gcnew InvalidOperationException(); } } } virtual property Object^ Value { Object^ get() { try { return ((DictionaryEntry^)_people[position])->Value; } catch (IndexOutOfRangeException^) { throw gcnew InvalidOperationException(); } } } }; public ref class People : IOrderedDictionary { private: ArrayList^ _people; public: People(int numItems) { _people = gcnew ArrayList(numItems); } int IndexOfKey(Object^ key) { for (int i = 0; i < _people->Count; i++) { if (((DictionaryEntry^)_people[i])->Key == key) return i; } // key not found, return -1. return -1; } virtual property Object^ default[Object^] { Object^ get(Object^ key) { return ((DictionaryEntry^)_people[IndexOfKey(key)])->Value; } void set(Object^ key, Object^ value) { _people[IndexOfKey(key)] = gcnew DictionaryEntry(key, value); } } // IOrderedDictionary Members virtual IDictionaryEnumerator^ GetEnumerator() { return gcnew PeopleEnum(_people); } virtual void Insert(int index, Object^ key, Object^ value) { if (IndexOfKey(key) != -1) { throw gcnew ArgumentException("An element with the same key already exists in the collection."); } _people->Insert(index, gcnew DictionaryEntry(key, value)); } virtual void RemoveAt(int index) { _people->RemoveAt(index); } virtual property Object^ default[int] { Object^ get(int index) { return ((DictionaryEntry^)_people[index])->Value; } void set(int index, Object^ value) { Object^ key = ((DictionaryEntry^)_people[index])->Key; _people[index] = gcnew DictionaryEntry(Keys, value); } } // IDictionary Members virtual void Add(Object^ key, Object^ value) { if (IndexOfKey(key) != -1) { throw gcnew ArgumentException("An element with the same key already exists in the collection."); } _people->Add(gcnew DictionaryEntry(key, value)); } virtual void Clear() { _people->Clear(); } virtual bool Contains(Object^ key) { if (IndexOfKey(key) == -1) { return false; } else { return true; } } virtual property bool IsFixedSize { bool get() { return false; } } virtual property bool IsReadOnly { bool get() { return false; } } virtual property ICollection^ Keys { ICollection^ get() { ArrayList^ KeyCollection = gcnew ArrayList(_people->Count); for (int i = 0; i < _people->Count; i++) { KeyCollection->Add( ((DictionaryEntry^)_people[i])->Key ); } return KeyCollection; } } virtual void Remove(Object^ key) { _people->RemoveAt(IndexOfKey(key)); } virtual property ICollection^ Values { ICollection ^get() { ArrayList^ ValueCollection = gcnew ArrayList(_people->Count); for (int i = 0; i < _people->Count; i++) { ValueCollection->Add( ((DictionaryEntry^)_people[i])->Value ); } return ValueCollection; } } // ICollection Members virtual void CopyTo(Array^ array, int index) { _people->CopyTo(array, index); } virtual property int Count { int get() { return _people->Count; } } virtual property bool IsSynchronized { bool get() { return _people->IsSynchronized; } } virtual property Object^ SyncRoot { Object^ get() { return _people->SyncRoot; } } // IEnumerable Members virtual IEnumerator^ IfcGetEnumerator() = IEnumerable::GetEnumerator { return (IEnumerator^) gcnew PeopleEnum(_people); } };
Available since 10
.NET Framework
Available since 2.0