SortedSet<T>クラス

SortedSet<int> sortedSet = new SortedSet<int>();

bool r1 = sortedSet.Add(2); // true
bool r2 = sortedSet.Add(3); // true
bool r3 = sortedSet.Add(3); // false
bool r4 = sortedSet.Add(1); // true

foreach (int item in sortedSet)
{
    Console.Write(item); // 1,2,3
}

実装しているインターフェイス

[System.Serializable]
public class SortedSet<T> :
    System.Collections.Generic.ICollection<T>
    System.Collections.Generic.IEnumerable<T>
    System.Collections.Generic.IReadOnlyCollection<T>
    System.Collections.Generic.ISet<T>
    System.Collections.ICollection
    System.Runtime.Serialization.IDeserializationCallback
    System.Runtime.Serialization.ISerializable

コンストラクタ

コンストラクタ  
SortedSet<T>()  
SortedSet<T>(IComparer<T>)  
SortedSet<T>(IEnumerable<T>)  
SortedSet<T>(IEnumerable<T>, IComparer<T>)  
SortedSet<T>(SerializationInfo, StreamingContext)  

SortedSet<T>(IComparer<T>)

public SortedSet (System.Collections.Generic.IComparer<T> comparer);

comparerで比較方法を指定することで、並べ替えの方法を変更できます。

プロパティ

プロパティ 内容
IComparer<T> Comparer  
int Count  
T Min コレクション内の最小値
T Max コレクション内の最大値

メソッド

メソッド 機能
Add(T) 指定の要素を追加できる
Remove(T) 指定の要素を削除できる
Clear() すべての要素を削除できる
Contains(T) 指定の要素が格納されているか判断できる
TryGetValue(T, T) 指定の要素を検索し、それと同じ値を取得できる
   
集合演算
メソッド 機能
IntersectWith(IEnumerable<T>) このコレクションを、指定のコレクションに含まれる要素だけに変更する
ExceptWith(IEnumerable<T>) このコレクションから、指定のコレクションに含まれる要素を削除する
UnionWith(IEnumerable<T>) このコレクションを、このコレクションまたは指定のコレクションのいずれか一方だけに存在する要素だけが格納されるように変更する
SymmetricExceptWith(IEnumerable<T>) このコレクションを、このコレクションまたは指定のコレクションのいずれかに存在する要素だけが格納されるように変更する

Add(T)

public bool Add (T item);
SortedSet<T>.Add(T) Method (System.Collections.Generic) | Microsoft Learn

要素を追加するたびに並べ替えられます。Add - sortedset.cs

Remove(T)

public bool Remove (T item);
SortedSet<T>.Remove(T) メソッド (System.Collections.Generic) | Microsoft Learn

要素が見つかり削除されたならばtrueが返されます。このメソッドの計算量はO(log n)です。

Microsoft Learnから検索