Topic : C# Collection - Sorted List in C# Programming Language: |SortedList Class:

SortedList Class:

The SortedList Class represents a Collection of Key and Value pairs that are Sorted by The Keys and Are Accessible by Key or by Index. The SortedList Collection Stores Key and Value Pairs in the Ascending Order of Key by Default.




SortedList : infobrother

A Sorted List is a Combination of Array and a Hash Table. It contains a list of Items that can be accessed using a Key or and Index. if we Access items using an Index, it is ArrayList, and If We Access Items using a Key, It is Hashtable. The Collection of Items is Always Sorted By the Key Value.

C# SortedList Class Provides some Properties and Methods to work with SortedList Collection. The Following tables lists some of the Commonly used Properties of the SortedList Class:



PropertiesDescription
CapacityGets or sets the capacity of a SortedList object.
CountGets the number of elements contained in a SortedList object.
IsFixedSizeGets a value indicating whether a SortedList object has a fixed size.
IsReadOnlyGets a value indicating whether a SortedList object is read-only.
itemGets and sets the value associated with a specific key in a SortedList object.
KeysGets the keys in a SortedList object.
ValuesGets the values in a SortedList object.


The Following tables lists some of the Commonly Used Methods of the SortedList Class:

MethodsDescription
Add()Adds an element with the specified key and value to a SortedList object.
Clear()Removes all elements from a SortedList object.
ContainsDetermines whether a SortedList object contains a specific key.
GetByIndex()Gets the value at the specified index of a SortedList object.
GetKey()Gets the key at the specified index of a SortedList object.
GetKeyList()Gets the keys in a SortedList object.
GetValueList()Gets the values in a SortedList object.
IndexOfKey()Returns the zero-based index of the specified key in a SortedList object.
IndexOfValue()Returns the zero-based index of the first occurrence of the specified value in a SortedList object.
Remove()Removes the element with the specified key from a SortedList object.
RemoveAt()Removes the element at the specified index of a SortedList object.
SetByIndex()Replaces the value at a specific index in a SortedList object.
TrimToSize()Sets the capacity to the actual number of elements in a SortedList object.
For complete list of properties and methods, please visit Microsoft's C# documentation.


Internally, SortedList Maintains two Object[] Array, One for the Keys and one for the Values. So, when we add key-value pair, it runs a Binary search using the key to find an appropriate index to store a key and value in respective arrays. it Re-arranges the elements when we remove the elements from it.


Let's have an Example to clear the concept about SortedList. In this example, we will add data in sortedList randomly, and we will see if our List can sort entered data in ascending order.



/*Example - SortedList - InfoBrother*/

using System;
using System.Collections;
public class MySortedList
{

    public static void Main()
    {
        // Creates new SortedList.
        SortedList SList = new SortedList();

        //Add some Elements in list:
        SList.Add(1255, "Hina");
        SList.Add(1258, "Usman");
        SList.Add(1257, "Kinat");
        SList.Add(1260, "Khizar");


        //Get the Total Number of Elements:
        Console.WriteLine(" Count:    {0}", SList.Count);

        //Get the Capacity of List:
        Console.WriteLine(" Capacity: {0}", SList.Capacity);
        Console.WriteLine(" Roll # and Student Name:");
        PrintKeysAndValues(SList);  //Print record:

        //add some more values:
        SList.Add(1256, "Aisha");
        SList.Add(1259, "Taimur");
        Console.WriteLine("\nUpdated Student Record:");
        PrintKeysAndValues(SList);  //Print record:

        //Get the Total Number of Elements:
        Console.WriteLine(" Count:    {0}", SList.Count);

        //Remove student whose ID # is 1256.
        Console.WriteLine("\n Remove Student Having ID #1256");
        SList.Remove(1256);
        PrintKeysAndValues(SList);  //Print record:

        //remove student from index # 1. i.e element # 2:
        Console.WriteLine("\n Remove Student From Index #1:");
        SList.RemoveAt(1);
        PrintKeysAndValues(SList);  //Print record:

        //Check for Already Existing Names:
        Console.WriteLine("\nDo We have Student ID #1225?  " + SList.Contains(1225));
        Console.WriteLine("Do We have Student ID #1259?  " + SList.Contains(1259));

        Console.ReadKey();
    }


    //Method to display record:
    public static void PrintKeysAndValues(SortedList myList)
    {
        Console.WriteLine(" Roll#     Name");

        //Access values using for loop:
        for (int i = 0; i < myList.Count; i++)
        {
            Console.WriteLine(" {0}:     {1}", myList.GetKey(i), myList.GetByIndex(i));
        }
    }
}


For complete list of properties and methods, please visit Microsoft's C# documentation.
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement