C# | Copying the SortedList elements to an Array Object Last Updated : 11 Jul, 2025 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://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.copyto?view=netframework-4.7.2 Comment K Kirti_Mangal Follow Improve K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Explore IntroductionC# Tutorial 2 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsClass and Objects in C# 4 min read Constructors in C# 5 min read C# Inheritance 3 min read Encapsulation in C# 2 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read C# | Method Parameters 7 min read Method Overriding in C# 7 min read Anonymous Method in C# 2 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read C# ArrayList Class 7 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 4 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like