How to create a shallow copy of SortedList Object in C# Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report SortedList.Clone() Method is used to create a shallow copy of a SortedList object. Syntax: public virtual object Clone(); Return Value: It returns a shallow copy of the SortedList object. The type of returned value will be Object. Note: A shallow copy of a collection copies only the elements of the collection, whether they are reference types or value types, but does not copy the objects that the references refer to. The references in the new collection point to the same objects that the references in the original collection point to. Below programs illustrate the use of above-discussed method: Example 1: CSharp // C# code to copy the // contents of one SortedList // to another SortedList. using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a SortedList SortedList mySL = new SortedList(); // Adding elements to SortedList mySL.Add(1, "C"); mySL.Add(2, "C++"); mySL.Add(3, "Java"); mySL.Add(4, "C#"); // Creating a shallow copy of mySL // we need to cast it explicitly SortedList myNewSL = (SortedList)mySL.Clone(); // displaying the elements of mySL Console.WriteLine("Elements of mySL: "); for (int i = 0; i < mySL.Count; i++) { Console.WriteLine("{0}:\t{1}", mySL.GetKey(i), mySL.GetByIndex(i)); } // displaying the elements of myNewSL Console.WriteLine("\nElements of myNewSL: "); for (int i = 0; i < myNewSL.Count; i++) { Console.WriteLine("{0}:\t{1}", myNewSL.GetKey(i), myNewSL.GetByIndex(i)); } } } Output: Elements of mySL: 1: C 2: C++ 3: Java 4: C# Elements of myNewSL: 1: C 2: C++ 3: Java 4: C# Example 2: CSharp // C# code to copy the // contents of one SortedList // to another SortedList. using System; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // Creating a SortedList SortedList mySL = new SortedList(); // Adding elements to SortedList mySL.Add(1, "HTML"); mySL.Add(2, "CSS"); mySL.Add(3, "PHP"); mySL.Add(4, "DBMS"); // Creating a shallow copy of mySL // we need to cast it explicitly SortedList myNewSL = (SortedList)mySL.Clone(); // checking for the equality // of References mySL and myNewSL Console.WriteLine("Reference Equals: {0}", Object.ReferenceEquals(mySL, myNewSL)); // displaying the elements of mySL Console.WriteLine("Elements of mySL: "); for (int i = 0; i < mySL.Count; i++) { Console.WriteLine("{0}:\t{1}", mySL.GetKey(i), mySL.GetByIndex(i)); } // displaying the elements of myNewSL Console.WriteLine("\nElements of myNewSL: "); for (int i = 0; i < myNewSL.Count; i++) { Console.WriteLine("{0}:\t{1}", myNewSL.GetKey(i), myNewSL.GetByIndex(i)); } // Replaces the values at // index 1 of mySL mySL.SetByIndex(1, "C#"); Console.WriteLine("\nAfter Replaces Elements in mySL\n"); // displaying the elements of myNewSL Console.WriteLine("\nElements of myNewSL: "); for (int i = 0; i < myNewSL.Count; i++) { Console.WriteLine("{0}:\t{1}", myNewSL.GetKey(i), myNewSL.GetByIndex(i)); } // displaying the elements of mySL Console.WriteLine("\nElements of mySL: "); for (int i = 0; i < mySL.Count; i++) { Console.WriteLine("{0}:\t{1}", mySL.GetKey(i), mySL.GetByIndex(i)); } } } Output: Reference Equals: False Elements of mySL: 1: HTML 2: CSS 3: PHP 4: DBMS Elements of myNewSL: 1: HTML 2: CSS 3: PHP 4: DBMS After Replaces Elements in mySL Elements of myNewSL: 1: HTML 2: CSS 3: PHP 4: DBMS Elements of mySL: 1: HTML 2: C# 3: PHP 4: DBMS Reference: https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.clone?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | How to create a shallow copy of the BitArray S SanchitDwivedi Follow Improve Article Tags : C# Similar Reads How to create a shallow copy of BitArray in C# BitArray.Clone() Method is used to create a shallow copy of the specified BitArray. A shallow copy of a collection copies only the elements of the collection irrespective of reference types or value types. But it does not copy the objects that the references refer to. The references in the new colle 2 min read How to create a shallow copy of ArrayList in C# ArrayList.Clone() Method is used to create a shallow copy of the specified ArrayList. A shallow copy of a collection copies only the elements of the collection irrespective of reference types or value types. But it does not copy the objects that the references refer to. The references in the new col 3 min read How to create a shallow copy of Hashtable in C# Hashtable.Clone Method is used to create a shallow copy of the Hashtable. When we make a shallow copy, only the elements of the collection get copied irrespective of its type, it doesn't copy the objects referred to by the references. Basically, it creates a new object and that object points to the 3 min read C# | How to create a shallow copy of the BitArray BitArray.Clone Method is used to create a shallow copy of the BitArray. This method only copies the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to.Syntax: public object Clone (); Example 1: Here we create an 3 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# | Getting the list of keys of a SortedList object SortedList.GetKeyList Method is used to get the list of keys in a SortedList object. Syntax: public virtual System.Collections.IList GetKeyList (); Return Value: It returns an IList object containing the keys in the SortedList object. Below programs illustrate the use of above-discussed method: Exam 2 min read Like