Tutorialspoint.dev

C# | SortedList Class

SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace.

Characteristics of SortedList Class:

  • A SortedList element can be accessed by its key or by its index.
  • A SortedList object internally maintains two arrays to store the elements of the list, i.e, one array for the keys and another array for the associated values.
  • A key cannot be null, but a value can be.
  • The capacity of a SortedList object is the number of elements the SortedList can hold.
  • A SortedList does not allow duplicate keys.
  • Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

Constructors

Constructor Description
SortedList() Initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the IComparable interface implemented by each key added to the SortedList object.
SortedList(IComparer) Initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the specified IComparer interface.
SortedList(IComparer, Int32) Initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted according to the specified IComparer interface.
SortedList(IDictionary) Initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the IComparable interface implemented by each key.
SortedList(IDictionary, IComparer) Initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the specified IComparer interface.
SortedList(Int32) Initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted according to the IComparable interface implemented by each key added to the SortedList object.

Example:

// C# Program to create a SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating object of SortedList
        // fslist is the SortedList object
        SortedList fslist = new SortedList();
  
        // Count property is used to get the
        // number of key/value pairs in fslist
        // It will give 0 as no pairs are present
        Console.WriteLine(fslist.Count);
    }
}

Output:

0

Properties

Property Description
Capacity Gets or sets the capacity of a SortedList object.
Count Gets the number of elements contained in a SortedList object.
IsFixedSize Gets a value indicating whether a SortedList object has a fixed size.
IsReadOnly Gets a value indicating whether a SortedList object is read-only.
IsSynchronized Gets a value indicating whether access to a SortedList object is synchronized (thread safe).
Item[Object] Gets and sets the value associated with a specific key in a SortedList object.
Keys Gets the keys in a SortedList object.
Values Gets the values in a SortedList object.

Example:



// C# Program to illustrate the
// properties of SortedList Class
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("1", "one");
        mySortedList.Add("2", "two");
        mySortedList.Add("3", "three");
        mySortedList.Add("4", "four");
        mySortedList.Add("5", "five");
  
        // Checking if a SortedList
        // object has a fixed size
        Console.WriteLine(mySortedList.IsFixedSize);
  
        // Checking if the created
        // SortedList is read-only or not
        Console.WriteLine(mySortedList.IsReadOnly);
    }
}

Output:

False
False

Methods

Method Description
Add(Object, Object) Adds an element with the specified key and value to a SortedList object.
Clear() Removes all elements from a SortedList object.
Clone() Creates a shallow copy of a SortedList object.
Contains(Object) Determines whether a SortedList object contains a specific key.
ContainsKey(Object) Determines whether a SortedList object contains a specific key.
ContainsValue(Object) Determines whether a SortedList object contains a specific value.
CopyTo(Array, Int32) Copies SortedList elements to a one-dimensional Array object, starting at the specified index in the array.
Equals(Object) Determines whether the specified object is equal to the current object.
GetByIndex(Int32) Gets the value at the specified index of a SortedList object.
GetEnumerator() Returns an IDictionaryEnumerator object that iterates through a SortedList object.
GetHashCode() Serves as the default hash function.
GetKey(Int32) Gets the key at the specified index of a SortedList object.
GetKeyList() Gets the keys in a SortedList object.
GetType() Gets the Type of the current instance.
GetValueList() Gets the values in a SortedList object.
IndexOfKey(Object) Returns the zero-based index of the specified key in a SortedList object.
IndexOfValue(Object) Returns the zero-based index of the first occurrence of the specified value in a SortedList object.
MemberwiseClone() Creates a shallow copy of the current Object.
Remove(Object) Removes the element with the specified key from a SortedList object.
RemoveAt(Int32) Removes the element at the specified index of a SortedList object.
SetByIndex(Int32, Object) Replaces the value at a specific index in a SortedList object.
Synchronized(SortedList) Returns a synchronized (thread-safe) wrapper for a SortedList object.
ToString() Returns a string that represents the current object.
TrimToSize() Sets the capacity to the actual number of elements in a SortedList object.

Example 1:

// C# code to remove all
// elements from a SortedList
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("1", "1st");
        mySortedList.Add("2", "2nd");
        mySortedList.Add("3", "3rd");
        mySortedList.Add("4", "4th");
        mySortedList.Add("5", "5th");
        mySortedList.Add("6", "6th");
        mySortedList.Add("7", "7th");
  
        // Displaying number of elements
        Console.WriteLine("Number of elements in SortedList is : "
                          + mySortedList.Count);
  
        // Displaying capacity
        Console.WriteLine("capacity of SortedList is : "
                          + mySortedList.Capacity);
  
        // Removing all elements from SortedList
        mySortedList.Clear();
  
        // Displaying number of elements
        Console.WriteLine("Number of elements in SortedList is : "
                          + mySortedList.Count);
  
        // Displaying capacity
        Console.WriteLine("capacity of SortedList is : "
                          + mySortedList.Capacity);
    }
}

Output:

Number of elements in SortedList is : 7
capacity of SortedList is : 16
Number of elements in SortedList is : 0
capacity of SortedList is : 16

Example 2:

// C# code to check if a SortedList
// object contains a specific value
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("1", "1st");
        mySortedList.Add("2", "2nd");
        mySortedList.Add("3", "3rd");
        mySortedList.Add("4", "4th");
  
        // Checking if a SortedList object
        // contains a specific value
        Console.WriteLine(mySortedList.ContainsValue(null));
    }
}

Output:

False

Reference:



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter