A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-sortedlist-class/ below:

C# SortedList Class - GeeksforGeeks

C# SortedList Class

Last Updated : 11 Jul, 2025

SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic SortedList is defined under System.Collections.

Example: This example demonstrates how to create a SortedList, add key-value pairs, and print them in ascending order based on the keys.

C#
// C# program to demonstrates SortedList
using System;
using System.Collections;

class Geeks {
    static void Main()
    {
        // Create a SortedList
        SortedList sl = new SortedList();

        // Add key/value pairs
        sl.Add("Java", 1);
        sl.Add("C++", 2);
        sl.Add("Python", 3);

        // Print the elements of the SortedList
        Console.WriteLine("SortedList elements:");

        foreach(DictionaryEntry i in sl)
        {
            // Corrected string interpolation
            Console.WriteLine($"{i.Key}: {i.Value}");
        }
    }
}

Output
SortedList elements:
C++: 2
Java: 1
Python: 3
Declarartion of SortedList

In C#, the declaration of SortedList can be done as:

SortedList sortedList = new SortedList();

SortedList<string, int> sortedList = new SortedList<string, int>();

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. type 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: This example demonstrates the Count property to get the number of key-value pairs.

C#
// C# Program to demonstrate 
// working of Count property
using System; 
using System.Collections; 

class Geeks { 

	public static void Main(String[] args) 
	{ 

		// Creating object of SortedList 	
		SortedList sL = new SortedList(); 

		// Count property is used to get the 
		// number of key-value pairs in s		
		Console.WriteLine(sL.Count); 
	} 
} 
Properties

The SortedList class provides several properties to access its state.

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: This example demonstrates how to check the IsFixedSize and IsReadOnly property.

C#
// C# program to demonstrate the working of 
// IsFixedSize anf IsReadOnly property
using System; 
using System.Collections; 

class Geeks { 

    public static void Main() { 

        // Creating a SortedList 
        SortedList sl = new SortedList(); 

        // Adding elements to SortedList 
        sl.Add("1", "one"); 
        sl.Add("2", "two"); 
        sl.Add("3", "three"); 
        sl.Add("4", "four"); 
        sl.Add("5", "five"); 

        // Checking if the SortedList has a fixed size 
        Console.WriteLine(sl.IsFixedSize); 

        // Checking if the SortedList is read-only 
        Console.WriteLine(sl.IsReadOnly); 
    } 
}
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: This example demonstrates the count and capacity of a SortedList.

C#
using System;
using System.Collections;

class Geeks {

    public static void Main() {

        // Creating a SortedList 
        SortedList sl = new SortedList();

        // Adding elements to SortedList 
        sl.Add("1", "1st");
        sl.Add("2", "2nd");
        sl.Add("3", "3rd");
        sl.Add("4", "4th");
        sl.Add("5", "5th");
        sl.Add("6", "6th");
        sl.Add("7", "7th");

        // Display number of elements
        Console.WriteLine("Elements count: " + sl.Count);

        // Display capacity 
        Console.WriteLine("Capacity: " + sl.Capacity);

        // Remove all elements
        sl.Clear();

        // Display number of elements after clearing
        Console.WriteLine("Elements count after Clear: " + sl.Count);

        // Display capacity after clearing
        Console.WriteLine("Capacity after Clear: " + sl.Capacity);
    }
}

Output
Elements count: 7
Capacity: 16
Elements count after Clear: 0
Capacity after Clear: 16

Example 2: This example demonstrates how to check if a SortedList contains a specific value.

C#
// C# Program  to demonstrates if a SortedList 
// object contains a specific value  
using System; 
using System.Collections; 

class Geeks { 
  
	public static void Main() 
	{ 

		// Creating an SortedList 
		SortedList sl = new SortedList(); 

		// Adding elements to SortedList 
		sl.Add("1", "1st"); 
		sl.Add("2", "2nd"); 
		sl.Add("3", "3rd"); 
		sl.Add("4", "4th"); 

		// Checking if a SortedList object 
		// contains a specific value 
		Console.WriteLine(sl.ContainsValue(null)); 
	} 
} 


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4