Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Collections.Generic.SortedDictionary: practical reference

System.Collections.Generic.SortedDictionary: practical reference: SortedDictionary<TKey,TValue> keeps keys ordered by an IComparer<TKey> with logarithmic lookup and update operations.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Collections.Generic.SortedDictionary /* select a documented member or constructor */

SortedDictionary<TKey,TValue> keeps keys ordered by an IComparer with logarithmic lookup and update operations.

Use it when a map must always enumerate entries in key order.

A Compare result of zero means the same key even when Equals disagrees, so the comparer must remain stable.

A null key is forbidden, and changing state that participates in key ordering corrupts the tree assumptions.

The complete program actually uses System.Collections.Generic.SortedDictionary; its exact expected stdout is 1,2.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var map = new SortedDictionary<int, string> { [2] = "b", [1] = "a" };
Console.WriteLine(string.Join(",", map.Keys));

Result

1,2

The complete program actually uses `System.Collections.Generic.SortedDictionary`; its exact expected stdout is `1,2`.

Sources