C# - SortedList.Clone() Method with Example

In this tutorial, we will learn about the C# SortedList.Clone() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 31, 2023

SortedList.Clone() Method

The SortedList.Clone() method 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.

C# Example of SortedList.Clone() Method

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.