Q:

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

belongs to collection: C# SortedList Class Programs

0

Syntax:

    object SortedList.Clone();

Parameter(s):

  • None

Return value:

It returns a shallow copy of the SortedList object.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 . . .

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# SortedList Class Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to remove all elements from a SortedLis... >>
<< C# program to copy SortedList elements to a one-di...