Q:

C# program to copy SortedList elements to a one-dimensional Array object (Example of CopyTo() Method)

belongs to collection: C# SortedList Class Programs

0

Syntax:

    void SortedList.CopyTo(Array array, int arrayIndex);

Parameter(s):

  • array: Used to copy elements of SortedList.
  • arrayIndex: The index in array at which copying begins

Return value:

It does not return any value.

Exception(s):

  • System.ArgumentNullException
  • System.ArgumentOutOfRangeException
  • System.ArgumentException
  • System.InvalidCastException
  •  

All Answers

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

Program:

The source code to copy SortedList elements to a one-dimensional Array 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 list = new SortedList();
     
        //Add elements to SortedList 
        list.Add(101, "India    ");
        list.Add(105, "America  ");
        list.Add(102, "Austrelia");
        list.Add(103, "Africa   ");
        list.Add(104, "Canada   ");

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

        //
        DictionaryEntry[] arr = new DictionaryEntry[list.Count];

        //Here we copy sorted list elements to specified index of array
        list.CopyTo(arr, 0);

        //Now we print array elements

        Console.WriteLine("Array Values:");
        for (int index = 0; index < arr.Length; index++)
        {
            Console.WriteLine("\t"+arr[index].Value);
        } 
    }
}

Output:

SortedList Values:
        India
        Austrelia
        Africa
        Canada
        America
Array Values:
        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 create a shallow copy of a SortedLis... >>
<< C# program to replace the value at a specific inde...