C# | Copying the SortedList elements to an Array Object Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destination of the DictionaryEntry objects copied from SortedList. The Array must have zero-based indexing. arrayindex: It is the zero-based index in array at which copying begins. Exceptions: ArgumentNullException: If the array is null. ArgumentOutOfRangeException: If the arrayindex is less than zero. ArgumentException: If the array is multidimensional or the number of elements in the source SortedList object is greater than the available space from arrayIndex to the end of the destination array. InvalidCastException: If the type of the source SortedList cannot be cast automatically to the type of the destination array. Below programs illustrate the use of above-discussed method: Example 1: CSharp // C# code to copy the SortedList elements // to a 1-D Array object, starting at the // specified index in the array using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a SortedList of integers SortedList mylist = new SortedList(); // Adding elements to SortedList mylist.Add("1", "C#"); mylist.Add("2", "Java"); mylist.Add("3", "DSA"); mylist.Add("4", "Python"); mylist.Add("5", "C"); // creating a 1-D array DictionaryEntry[] myArr = new DictionaryEntry[mylist.Count]; // Copying SortedList to Array // instance at the specified index mylist.CopyTo(myArr, 0); // Displaying elements in myArr for (int i = 0; i < myArr.Length; i++) { Console.WriteLine(myArr[i].Key + "-->" + myArr[i].Value); } } } Output: 1-->C# 2-->Java 3-->DSA 4-->Python 5-->C Example 2: CSharp // C# code to copy the SortedList elements // to a 1-D Array object, starting at the // specified index in the array using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a SortedList of integers SortedList mylist = new SortedList(); // Adding elements to SortedList mylist.Add("1st", "Ram"); mylist.Add("2nd", "Shyam"); mylist.Add("3rd", "Rohit"); mylist.Add("4th", "Manish"); mylist.Add("5th", "Vikas"); // creating a 1-D array DictionaryEntry[] myArr = new DictionaryEntry[mylist.Count]; // Copying SortedList to Array // instance at the specified index // This will raise "ArgumentOutOfRangeException" // as index is less than 0 mylist.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: Non-negative number required. Parameter name: arrayIndex Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.copyto?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Copying the SortedList elements to an Array Object K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Copying the elements of ArrayList to a new array ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra 2 min read C# | Copy the entire LinkedList<T> to Array LinkedList<T>.CopyTo(T[], Int32) method is used to copy the entire LinkedList<T> to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : It is the one-dimensional Array that is the 2 min read C# | Sort the elements in the ArrayList ArrayList.Sort Method is used to sorts the elements in the ArrayList. There are total 3 methods in the overload list of this method as follows: Sort()Sort(IComparer)Sort(Int32, Int32, IComparer)Sort() This method is used to Sort the elements in the entire ArrayList. It uses the QuickSort algorithm t 4 min read C# | Getting the list of Values of a SortedList object SortedList.GetValueList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetValueList (); Return Value: It returns an IList object containing the values in the SortedList object. Below programs illustrate the use of above-discussed method 2 min read C# | How to create 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. Properties of SortedList: Internally the object of SortedList maintains two arr 2 min read C# | How to copy the entire ArrayList to a one-dimensional Array ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. Syntax: public virtual void CopyTo (Array array); Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList 3 min read C# SortedList Class SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic 6 min read C# SortedSet Class SortedSet class in C# represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace.In C#, the SortedSet class can be used to store, remove, or view elements.It maintains ascending order and does not store duplicate elements.It is suggested to 5 min read C# SortedList In C#, SortedList is a collection of key-value pairs sorted according to keys. By default, this collection sorts ascendingly It is of both generic and non-generic type of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic SortedList is defin 7 min read C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe 8 min read Like