How to create a shallow copy of SortedList Object in C#

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)); 
              
            
          
    

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)); 
            }
              
          
    

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.