Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a shallow copy of SortedList Object in C#?
The shallow copy of a SortedList object in C# creates a new SortedList instance with the same key-value pairs as the original, but both collections share references to the same objects. This is accomplished using the Clone() method.
Syntax
Following is the syntax for creating a shallow copy of SortedList −
SortedList clonedList = (SortedList)originalList.Clone();
What is a Shallow Copy?
A shallow copy creates a new collection object, but the elements inside both the original and copied collections point to the same memory locations. For value types like strings and integers, this behaves like a deep copy. For reference types (objects), changes to the object's properties will be reflected in both collections.
Using Clone() Method
Example
using System;
using System.Collections;
public class Demo {
public static void Main(string[] args) {
SortedList list = new SortedList();
list.Add("A", "Jacob");
list.Add("B", "Sam");
list.Add("C", "Tom");
list.Add("D", "John");
list.Add("E", "Tim");
Console.WriteLine("Original SortedList elements:");
foreach(DictionaryEntry d in list) {
Console.WriteLine(d.Key + " " + d.Value);
}
SortedList list2 = (SortedList)list.Clone();
Console.WriteLine("\nCloned SortedList elements:");
foreach(DictionaryEntry d in list2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nAre they the same reference? " + (list == list2));
Console.WriteLine("Do they have the same content? " + (list.Count == list2.Count));
}
}
The output of the above code is −
Original SortedList elements: A Jacob B Sam C Tom D John E Tim Cloned SortedList elements: A Jacob B Sam C Tom D John E Tim Are they the same reference? False Do they have the same content? True
Demonstrating Shallow Copy Behavior
Example
using System;
using System.Collections;
public class Demo {
public static void Main(string[] args) {
SortedList departments = new SortedList();
departments.Add("One", "IT");
departments.Add("Two", "Operations");
departments.Add("Three", "Marketing");
departments.Add("Four", "Finance");
Console.WriteLine("Original SortedList:");
foreach(DictionaryEntry d in departments) {
Console.WriteLine(d.Key + " " + d.Value);
}
SortedList clonedDepartments = (SortedList)departments.Clone();
Console.WriteLine("\nCloned SortedList:");
foreach(DictionaryEntry d in clonedDepartments) {
Console.WriteLine(d.Key + " " + d.Value);
}
// Modify original - clone remains unchanged
departments["One"] = "Information Technology";
Console.WriteLine("\nAfter modifying original:");
Console.WriteLine("Original 'One': " + departments["One"]);
Console.WriteLine("Cloned 'One': " + clonedDepartments["One"]);
}
}
The output of the above code is −
Original SortedList: Four Finance One IT Three Marketing Two Operations Cloned SortedList: Four Finance One IT Three Marketing Two Operations After modifying original: Original 'One': Information Technology Cloned 'One': IT
Key Characteristics of Shallow Copy
| Aspect | Behavior |
|---|---|
| Collection Structure | New SortedList instance is created |
| Value Types | Independent copies (strings, integers, etc.) |
| Reference Types | Same object references shared between collections |
| Performance | Fast operation, only copies references |
Conclusion
The Clone()
