C# | Remove the entry at specified index from OrderedDictionary (original) (raw)
Last Updated : 01 Feb, 2019
OrderedDictionary.RemoveAt(Int32) method is used to remove the entry at the specified index from the OrderedDictionary collection.Syntax:
public void RemoveAt (int index);
Here, index is the zero-based index of the entry to remove.Exceptions:
- NotSupportedException : If the OrderedDictionary collection is read-only.
- ArgumentOutOfRangeException : If the index is less than zero OR index is equal to or greater than Count.
Below given are some examples to understand the implementation in a better way: Example 1:
CSHARP `
// C# code to remove the entry at // the specified index from the // OrderedDictionary using System; using System.Collections; using System.Collections.Specialized;
class GFG {
// Driver method
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("key1", "value1");
myDict.Add("key2", "value2");
myDict.Add("key3", "value3");
myDict.Add("key4", "value4");
myDict.Add("key5", "value5");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
// Removing the entry at the specified
// index from the OrderedDictionary
myDict.RemoveAt(3);
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
}
}
`
Output:
Number of elements are : 5 key1 -- value1 key2 -- value2 key3 -- value3 key4 -- value4 key5 -- value5 Number of elements are : 4 key1 -- value1 key2 -- value2 key3 -- value3 key5 -- value5
Example 2:
CSHARP `
// C# code to remove the entry at // the specified index from the // OrderedDictionary using System; using System.Collections; using System.Collections.Specialized;
class GFG {
// Driver method
public static void Main()
{
// Creating a orderedDictionary named myDict
OrderedDictionary myDict = new OrderedDictionary();
// Adding key and value in myDict
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
// Removing the entry at the specified
// index from the OrderedDictionary
// This should raise "ArgumentOutOfRangeException"
// as index is less than 0
myDict.RemoveAt(-2);
// Displaying the number of element initially
Console.WriteLine("Number of elements are : "
+ myDict.Count);
// Displaying the elements in myDict
foreach(DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " -- " + de.Value);
}
}
`
Runtime Error:
Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
Note: The entries that follow the removed entry move up to occupy the vacated spot and the indexes of the entries that move are also updated.Reference:
Similar Reads
- C# | Remove entry with specified key from OrderedDictionary OrderedDictionary.Remove(Object) method is used to remove entry with the specified key from the OrderedDictionary collection. Syntax: public void Remove (object key); Here, key is the key of the entry to remove. Exceptions: NotSupportedException : If the OrderedDictionary collection is read-only. Ar 3 min read
- C# | Remove the entry with specified key from ListDictionary ListDictionary.Remove(Object) method is used to remove the entry with the specified key from the ListDictionary. Syntax: public void Remove (object key); Here, key is the key of the entry which is to be removed. Exception: This method will give ArgumentNullException if the key is null. Below are the 3 min read
- C# | Removing the specified key entry from HybridDictionary HybridDictionary.Remove(Object) method is used to remove the entry with the specified key from the HybridDictionary. Syntax: public void Remove (object key); Here, key is the key of the entry to remove. Exception: This method throws ArgumentNullException if the key is null. Below given are some exam 3 min read
- C# | Remove all elements from OrderedDictionary OrderedDictionary.Clear method is used to remove all elements from the OrderedDictionary collection. Syntax: public void Clear (); Exception: If the OrderedDictionary collection is read-only then it will throw NotSupportedException. Below given are some examples to understand the implementation in a 2 min read
- C# | Remove all entries from the ListDictionary ListDictionary.Clear method is used to remove the all entries from the ListDictionary. Syntax: public void Clear (); Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to remove all entries // from the ListDictionary using System; using Syste 3 min read
- C# | Insert a new entry in OrderedDictionary with specified key and value OrderedDictionary.Insert(Int32, Object, Object) Method is used to inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index: It is the zero-based index of type 3 min read
- C# | Copy OrderedDictionary elements to Array instance at the specified index OrderedDictionary.CopyTo(Array, Int32) method is used to copy the OrderedDictionary elements to a one-dimensional Array object at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array: It is the one-dimensional Array which is the destination of the DictionaryEnt 2 min read
- C# | Remove from the specified index of a SortedList SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.RemoveAt(Int32) method is used to remove the element at the specifie 4 min read
- C# | Insert into OrderedDictionary with key and value at specified index OrderedDictionary.Insert(Int32, Object, Object) method is used to insert a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index : It is the zero-based index at whic 3 min read
- C# | Get a read-only copy of the OrderedDictionary OrderedDictionary.AsReadOnly method returns a read-only copy of the current OrderedDictionary collection. Syntax: public System.Collections.Specialized.OrderedDictionary AsReadOnly (); Return Value: A read-only copy of the current OrderedDictionary collection. Below given are some examples to unders 2 min read