C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
The SortedList
class in C# is part of the System.Collections
namespace and represents a collection of key-value pairs that are sorted by the keys. This tutorial will cover the following topics related to the SortedList
class in C#:
Let's begin!
To create a new instance of the SortedList
class, use the SortedList
constructor:
using System.Collections; SortedList sortedList = new SortedList();
To add an element to a SortedList
, use the Add
method:
sortedList.Add("one", 1); sortedList.Add("two", 2); sortedList.Add("three", 3);
In this example, we add three key-value pairs to the SortedList
. The keys are strings and the values are integers.
To access an element in a SortedList
by its key, use the indexer:
int value = (int)sortedList["two"]; // Cast the value to int Console.WriteLine(value); // Output: 2
To access a key or value by its index, use the GetKey
and GetByIndex
methods, respectively:
string key = (string)sortedList.GetKey(1); int value = (int)sortedList.GetByIndex(1); Console.WriteLine($"{key}: {value}"); // Output: two: 2
To remove an element from a SortedList
by its key, use the Remove
method:
sortedList.Remove("two");
To remove an element from a SortedList
by its index, use the RemoveAt
method:
sortedList.RemoveAt(1);
To loop through the keys and values of a SortedList
, you can use a foreach
loop:
foreach (DictionaryEntry entry in sortedList) { Console.WriteLine($"{entry.Key}: {entry.Value}"); }
Alternatively, you can use the for
loop and the Count
property to iterate through the keys and values by index:
for (int i = 0; i < sortedList.Count; i++) { string key = (string)sortedList.GetKey(i); int value = (int)sortedList.GetByIndex(i); Console.WriteLine($"{key}: {value}"); }
That's it! You've now learned how to use the SortedList
class in C# to store and manipulate key-value pairs that are sorted by the keys. The SortedList
class is useful for scenarios where you need to maintain a sorted collection of key-value pairs and have fast key-based lookups. Note that the SortedList
class is not type-safe and requires casting when working with keys and values. If you need a type-safe sorted collection, consider using the SortedDictionary<TKey, TValue>
class from the System.Collections.Generic
namespace instead.
How to use SortedList in C#
SortedList
is a collection that maintains elements in sorted order based on their keys.
using System; using System.Collections; class Program { static void Main() { SortedList sortedList = new SortedList(); sortedList.Add(3, "Three"); sortedList.Add(1, "One"); sortedList.Add(2, "Two"); foreach (DictionaryEntry entry in sortedList) { Console.WriteLine($"{entry.Key}: {entry.Value}"); } } }
Custom sorting with IComparer in C# SortedList
Implement IComparer
for custom sorting logic.
class CustomComparer : IComparer { public int Compare(object x, object y) { // Custom comparison logic } }
C# SortedList and LINQ queries
SortedList
can be used with LINQ for various querying operations.
var results = sortedList.Cast<DictionaryEntry>().Where(entry => entry.Key.Equals(2));