Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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:




// 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:




// 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:



Last Updated : 01 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads