C# | Copy ListDictionary to Array instance at the specified index (original) (raw)
Last Updated : 01 Feb, 2019
ListDictionary.CopyTo(Array, Int32) method is used to copy the ListDictionary entries to a one-dimensional Array instance 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 DictionaryEntry objects copied from ListDictionary. The Array must have zero-based indexing.index : The zero-based index in array at which copying begins.
Exceptions:
- ArgumentNullException : If the array is null.
- ArgumentOutOfRangeException : If the index is less than zero.
- InvalidCastException : If the type of the source ListDictionary cannot be cast automatically to the type of the destination array.
- ArgumentException : If the array is multidimensional OR the number of elements in the source ListDictionary is greater than the available space from index to the end of the destination array.
Below given are some examples to understand the implementation in a better way:Example 1:
CSHARP `
// C# code to copy ListDictionary to // Array instance at the specified index using System; using System.Collections; using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a ListDictionary named myDict
ListDictionary myDict = new ListDictionary();
myDict.Add("Australia", "Canberra");
myDict.Add("Belgium", "Brussels");
myDict.Add("Netherlands", "Amsterdam");
myDict.Add("China", "Beijing");
myDict.Add("Russia", "Moscow");
myDict.Add("India", "New Delhi");
DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
// Copying ListDictionary to Array
// instance at the specified index
myDict.CopyTo(myArr, 0);
// Displaying elements in myArr
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine(myArr[i].Key + "-->" + myArr[i].Value);
}
}
}
`
Output:
Australia-->Canberra Belgium-->Brussels Netherlands-->Amsterdam China-->Beijing Russia-->Moscow India-->New Delhi
Example 2:
CSHARP `
// C# code to copy ListDictionary to // Array instance at the specified index using System; using System.Collections; using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a ListDictionary named myDict
ListDictionary myDict = new ListDictionary();
myDict.Add("Australia", "Canberra");
myDict.Add("Belgium", "Brussels");
myDict.Add("Netherlands", "Amsterdam");
myDict.Add("China", "Beijing");
myDict.Add("Russia", "Moscow");
myDict.Add("India", "New Delhi");
DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
// Copying ListDictionary to Array
// instance at the specified index
// This should raise "ArgumentOutOfRangeException"
// as index is less than 0
myDict.CopyTo(myArr, -2);
// Displaying elements in myArr
for (int i = 0; i < myArr.Length; i++) {
Console.WriteLine(myArr[i].Key + "-->" + myArr[i].Value);
}
}
}
`
Runtime Error:
Unhandled Exception: System.ArgumentOutOfRangeException: Index is less than zero. Parameter name: index
Note:
- The elements are copied to the Array in the same order in which the enumerator iterates through the ListDictionary.
- To copy only the keys in the ListDictionary, use ListDictionary.Keys.CopyTo.
- To copy only the values in the ListDictionary, use ListDictionary.Values.CopyTo.
- This method is an O(n) operation, where n is Count.
Reference:
Similar Reads
- 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# | Copy StringDictionary to Array at the specified index StringDictionary.CopyTo(Array, Int32) method is used to copy the string dictionary values to a one-dimensional Array instance at the specified index. Syntax: public virtual void CopyTo (Array array, int index); Parameters: array: It is the one-dimensional Array which is the destination of the values 3 min read
- C# | Add the specified key and value into the ListDictionary ListDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the ListDictionary. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the entry to add. The value can be null. Exceptions: Arg 2 min read
- C# | Copying the HybridDictionary entries to an Array Instance HybridDictionary.CopyTo(Array, Int32) method is used to copy the HybridDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry obje 3 min read
- C# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis 3 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# | Copy StringCollection at the specified index of array StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.CopyTo(String[], Int32) method is used to copy the entire StringCollection values 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# | Get or set the value associated with specified key in ListDictionary ListDictionary.Item[Object] property is used to get or set the value associated with the specified key. Syntax: public object this[object key] { get; set; } Here, key is the key whose value to get or set. Return Value : The value associated with the specified key. If the specified key is not found, 2 min read
- C# | How to insert the elements of a collection into the List at the specified index List.InsertRange(Int32, IEnumerable) Method is used to insert the elements of a collection into the List at the specified index. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid 3 min read