C# program to create a shallow copy of a SortedList object (Example of Clone() Method)

C# SortedList.Clone() Method: Here, we are going to create a shallow copy of a SortedList object in C#.Net?
Submitted by Nidhi, on May 02, 2021

The Clone() method of SortedList class is used to create shallow copy SortedList elements. This method returns object type, then here we need to typecast it to SortedList.

Syntax:

    object SortedList.Clone();

Parameter(s):

  • None

Return value:

It returns a shallow copy of the SortedList object.

Program:

The source code to create a shallow copy of a SortedList object is given below. The given program is compiled and executed successfully.

using System;
using System.Collections;

class SortedListEx
{
    //Entry point of Program
    static public void Main()
    {
        //Creation of SortedList object
        SortedList list1 = new SortedList();
        SortedList list2;

        //Add elements to SortedList 
        list1.Add(101, "India    ");
        list1.Add(105, "America  ");
        list1.Add(102, "Austrelia");
        list1.Add(103, "Africa   ");
        list1.Add(104, "Canada   ");

        Console.WriteLine("List1 Values:");
        foreach (string value in list1.Values)
        {
            Console.WriteLine("\t" + value);
        }

        list2 = (SortedList)list1.Clone();

        Console.WriteLine("List2 Values(Shallow copy of list1):");
        foreach (string value in list2.Values)
        {
            Console.WriteLine("\t" + value);
        }
    }
}

Output:

List1 Values:
        India
        Austrelia
        Africa
        Canada
        America
List2 Values(Shallow copy of list1):
        India
        Austrelia
        Africa
        Canada
        America
Press any key to continue . . .

C# SortedList Class Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.