Sortedlist.Clone() Method In C#

The clone function in C# proves to be beneficial for generating a superficial duplicate of the specified SortedList object. Any modifications applied to the initial SortedList will be visible in the newly produced superficial duplicate, which is generated through the Clone function. The fresh superficial duplicate of the primary sorted list will encompass the pointers to the elements existing in the original list. This technique is employed when the developer aims to replicate the list while preserving the integrity of the original list.

Modifications applied to the initial SortedList will be mirrored in the shallow duplicate, however, any alterations made to the shallow list will not impact the original SortedList object.

Syntax:

It has the following syntax:

Example

SortedList clonedList = (SortedList)originalList.Clone();

This function does not require any arguments. It produces an object that needs to be explicitly converted to a sortedList or a similar type.

Example 1:

Let's consider a scenario to demonstrate the Clone function in C#.

Example

using System;
using System.Collections;
class Program
{
    static void Main(string[] args)
    {
        SortedList originalList = new SortedList();
        originalList.Add("1", "Item 1");
        originalList.Add("2", "Item 2");
        originalList.Add("3", "Item 3");
        Console.WriteLine("Original List:");
        DisplayList(originalList);
        // Take a shallow copy of the original list
        SortedList shallowCopy = (SortedList)originalList.Clone();

        // Display the shallow copy
        Console.WriteLine("\nShallow Copy:");
        DisplayList(shallowCopy);

        // Change the original list
        originalList["1"] = "Updated Item 1";

        // It will display the original list after modification
        Console.WriteLine("\nOriginal List after Modification:");
        DisplayList(originalList);

        // This function display the shallow copy to observe if it's affected by the modification
        Console.WriteLine("\nShallow Copy after Modification of Original List:");
        DisplayList(shallowCopy);

        // Change the shallow copy
        shallowCopy["2"] = "Updated Item 2";

        // It will display the shallow copy after modification
        Console.WriteLine("\nShallow Copy after Modification:");
        DisplayList(shallowCopy);

        // It will display the original list to observe if it's affected by the modification to shallow copy
        Console.WriteLine("\nOriginal List after Modification of Shallow Copy:");
        DisplayList(originalList);
    }

    static void DisplayList(SortedList list)
    {
        foreach (DictionaryEntry entry in list)
        {
            Console.WriteLine($"{entry.Key}: {entry.Value}");
        }
    }
}

Output:

The <style> element represents a diagram template with a distinctive design. It includes a CSS class for styling the diagram container with a gradient background, rounded borders, padding, margin, and centered text alignment. Additionally, there are specific styles for the icon and text elements within the diagram. The icon is set to a larger font size with some spacing at the bottom, while the text is styled with a specific color and font size. This structure provides a visually appealing and well-organized layout for displaying various types of diagrams.

Explanation:

This code snippet demonstrates the implementation of the Clone method in C#. Within this example, the variables employed are originalList, an object responsible for holding key-value pairs, and a shallow duplicate that signifies a shallow copy of the initial list. The program includes a function titled DisplayList(SortedList list) to exhibit the contents within the specified list.

We generated a unique Sorted List and populated it with various elements. Following this, the items are showcased utilizing the designated function. Subsequently, a duplicate of the initial Sorted List is created using the Clone method and labeled as shallowCopy. Post this action, adjustments are made to the original list, and these alterations are mirrored in the shallowCopy due to the shared references between the two Sorted Lists. Ultimately, modifications made to the shallowCopy do not impact the originalList.

Example 2:

Let's consider another instance to showcase the Clone function in C#.

Example

using System;
using System.Collections;
class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Grade { get; set; }

    public Student(string name, int age, string grade)
    {
        Name = name;
        Age = age;
        Grade = grade;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Original database of student records
        SortedList studentRecords = new SortedList();
        studentRecords.Add("1001", new Student("Ramu", 18, "A"));
        studentRecords.Add("1002", new Student("Raju", 17, "B"));
        studentRecords.Add("1003", new Student("Ravi", 16, "C"));
        Console.WriteLine("Original Database:");
        DisplayStudentRecords(studentRecords);
        // Create a shallow copy of the original database
        SortedList shallowCopy = (SortedList)studentRecords.Clone();
        // Display shallow copy
        Console.WriteLine("\nShallow Copy:");
        DisplayStudentRecords(shallowCopy);
        ((Student)studentRecords["1001"]).Grade = "A+";
        Console.WriteLine("\nOriginal Database after Modification:");
        DisplayStudentRecords(studentRecords);
        Console.WriteLine("\nShallow Copy after Modification of Original Database:");
        DisplayStudentRecords(shallowCopy);
        ((Student)shallowCopy["1002"]).Name = "Robert";
        Console.WriteLine("\nShallow Copy after Modification:");
        DisplayStudentRecords(shallowCopy);
        Console.WriteLine("\nOriginal Database after Modification of Shallow Copy:");
        DisplayStudentRecords(studentRecords);
    }

    static void DisplayStudentRecords(SortedList records)
    {
        foreach (DictionaryEntry entry in records)
        {
            string id = (string)entry.Key;
            Student student = (Student)entry.Value;
            Console.WriteLine($"ID: {id}, Name: {student.Name}, Age: {student.Age}, Grade: {student.Grade}");
        }
    }
}

Output:

The CSS code snippet provided below illustrates the styling for a placeholder diagram:

Example

.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }

Explanation:

This C# script showcases the functionality of the SortedList.Clone technique. Initially, there exists student information within the primary database. To adjust this data, the instructor needs to introduce certain modifications. Following this, a shallow duplicate of the complete initial database is generated as a precaution against potential erroneous alterations that could render the original database unusable. Consequently, the Clone method is enlisted to produce this shallow duplicate. Subsequent adjustments are implemented within the shallow duplicate. Notably, alterations applied to the primary database are mirrored in the shallow duplicate, such as updating a student's academic performance. However, specific changes executed in the shallow copy, such as altering student names, do not impact the original database.

Input Required

This code uses input(). Please provide values below: