SortedDictionaryとは? わかりやすく解説

SortedDictionary コンストラクタ ()

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

空で、キーの型の既定の IComparer 実装を使用する、SortedDictionary クラスの新しいインスタンスを初期化します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Dim instance As New SortedDictionary(Of
 TKey, TValue)
public SortedDictionary ()
public:
SortedDictionary ()
public SortedDictionary ()
public function SortedDictionary ()
解説解説
使用例使用例

文字列キーを含む文字列の空の SortedDictionary を作成し、Add メソッドを使用していくつかの要素を追加するコード例を次に示します。この例では、重複するキーを追加しようとすると、Add メソッドが ArgumentException をスローすることを示します。

このコード例は、SortedDictionary クラスのトピックで取り上げているコード例の一部分です。

' Create a new sorted dictionary of strings, with string 
' keys. 
Dim openWith As New SortedDictionary(Of
 String, String)

' Add some elements to the dictionary. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt""
 already exists.")
End Try
// Create a new sorted dictionary of strings, with string
// keys.
SortedDictionary<string, string> openWith
 = 
    new SortedDictionary<string, string>();

// Add some elements to the dictionary. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is 
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortedDictionary ジェネリック クラス
SortedDictionary メンバ
System.Collections.Generic 名前空間
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス

SortedDictionary コンストラクタ

SortedDictionary クラスの新しいインスタンスを初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
SortedDictionary () 空で、キーの型の既定の IComparer 実装を使用する、SortedDictionary クラスの新しいインスタンスを初期化します。
SortedDictionary (ジェネリック IComparer) 空で、指定した IComparer を使用してキーを比較する、SortedDictionary クラスの新しいインスタンスを初期化します。
SortedDictionary (ジェネリック IDictionary) 指定した IDictionary から要素をコピーして格納し、キーの型の既定の IComparer 実装を使用する、SortedDictionary クラスの新しいインスタンスを初期化します。
SortedDictionary (ジェネリック IDictionary, ジェネリック IComparer) 指定した IDictionary から要素をコピーして格納し、指定した IComparer 実装を使用してキーを比較する、SortedDictionary クラスの新しいインスタンスを初期化します。
参照参照

関連項目

SortedDictionary ジェネリック クラス
SortedDictionary メンバ
System.Collections.Generic 名前空間
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス

SortedDictionary コンストラクタ (ジェネリック IDictionary)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

指定した IDictionary から要素をコピーして格納し、キーの型の既定の IComparer 実装を使用する、SortedDictionary クラスの新しいインスタンスを初期化します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    dictionary As IDictionary(Of TKey, TValue)
 _
)
Dim dictionary As IDictionary(Of
 TKey, TValue)

Dim instance As New SortedDictionary(Of
 TKey, TValue)(dictionary)
public SortedDictionary (
    IDictionary<TKey,TValue> dictionary
)
public:
SortedDictionary (
    IDictionary<TKey, TValue>^ dictionary
)
public SortedDictionary (
    IDictionary<TKey,TValue> dictionary
)
public function SortedDictionary (
    dictionary : IDictionary<TKey,TValue>
)

パラメータ

dictionary

新しい SortedDictionary に要素がコピーされた IDictionary。

例外例外
例外の種類条件

ArgumentNullException

dictionary が null 参照 (Visual Basic では Nothing) です。

ArgumentException

dictionary に、1 つ以上の重複するキーが格納されています。

解説解説
使用例使用例

Dictionary を SortedDictionary(ジェネリック IComparer) コンストラクタに渡すことにより、SortedDictionary を使用して、Dictionary 内の情報の並べ替えられたコピーを作成する方法を次のコード例に示します。

Imports System
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main()
 

        ' Create a new Dictionary of strings, with string 
        ' keys.
        Dim openWith As New
 Dictionary(Of String, String)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' Create a SortedDictionary of strings with string keys, 
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New
 SortedDictionary(Of String, String)(openWith)

        ' List the sorted contents of the copy.
        Console.WriteLine()
        For Each kvp As
 KeyValuePair(Of String, String)
 In copy
            Console.WriteLine("Key = {0}, Value = {1}",
 _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys.
        //
        Dictionary<string, string> openWith
 = 
                                  new Dictionary<string,
 string>();
        
        // Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
        
        // Create a SortedDictionary of strings with string keys, 
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string>
 copy = 
                  new SortedDictionary<string,
 string>(openWith);

        // List the contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string>
 kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}", 
               kvp.Key, kvp.Value);
        }
    }
}

/* This code example produces the following output:

Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedDictionary コンストラクタ (ジェネリック IComparer)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

空で、指定した IComparer を使用してキーを比較する、SortedDictionary クラスの新しいインスタンスを初期化します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    comparer As IComparer(Of TKey) _
)
Dim comparer As IComparer(Of
 TKey)

Dim instance As New SortedDictionary(Of
 TKey, TValue)(comparer)
public SortedDictionary (
    IComparer<TKey> comparer
)
public:
SortedDictionary (
    IComparer<TKey>^ comparer
)
public SortedDictionary (
    IComparer<TKey> comparer
)
public function SortedDictionary (
    comparer : IComparer<TKey>
)

パラメータ

comparer

キーの比較時に使用する IComparer 実装。キーの型の既定の Comparer を使用する場合は null 参照 (Visual Basic では Nothing)。

解説解説

SortedDictionary 内のすべてのキーは指定した比較演算子に従って一意である必要があります。

SortedDictionary は、キーの比較を実行するために比較演算子の実装を必要とします。comparer が null 参照 (Visual Basic では Nothing) の場合、このコンストラクタは、既定のジェネリック等値比較演算子である Comparer.Default を使用します。型 TKey が System.IComparable ジェネリック インターフェイスを実装している場合は、既定の比較演算子でその実装が使用されます。

このコンストラクタは O(1) 操作です。

使用例使用例

現在のカルチャの大文字小文字を区別しない比較演算子を使って SortedDictionary を作成するコード例を次に示します。この例では、小文字のキーを含む要素と大文字のキーを含む要素を合わせて 4 つの要素を追加します。次に、既存のキーとの違いが大文字と小文字の違いだけであるキーを含む要素の追加を試行し、結果の例外をキャッチし、エラー メッセージを表示します。最後に、大文字小文字を区別しない並べ替え順で要素を表示します。

Imports System
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main()
 

        ' Create a new SortedDictionary of strings, with string keys
 
        ' and a case-insensitive comparer for the current culture.
        Dim openWith As New
 SortedDictionary(Of String, String)(
 _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' Try to add a fifth element with a key that is the same 
        ' except for case; this would be allowed with the default
        ' comparer.
        Try
            openWith.Add("BMP", "paint.exe")
        Catch ex As ArgumentException
            Console.WriteLine(vbLf & "BMP is already in the
 dictionary.")
        End Try
        
        ' List the contents of the sorted dictionary.
        Console.WriteLine()
        For Each kvp As
 KeyValuePair(Of String, String)
 In openWith
            Console.WriteLine("Key = {0}, Value = {1}",
 _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'BMP is already in the dictionary.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new SortedDictionary of strings, with string keys
 
        // and a case-insensitive comparer for the current culture.
        SortedDictionary<string, string>
 openWith = 
                      new SortedDictionary<string,
 string>( 
                          StringComparer.CurrentCultureIgnoreCase);
        
        // Add some elements to the dictionary.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // Try to add a fifth element with a key that is the same 
        // except for case; this would be allowed with the default
        // comparer.
        try
        {
            openWith.Add("BMP", "paint.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("\nBMP is already in the dictionary.");
        }
        
        // List the contents of the sorted dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string>
 kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, 
                kvp.Value);
        }
    }
}

/* This code example produces the following output:

BMP is already in the dictionary.

Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedDictionary コンストラクタ (ジェネリック IDictionary, ジェネリック IComparer)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

指定した IDictionary から要素をコピーして格納し、指定した IComparer 実装を使用してキーを比較する、SortedDictionary クラスの新しいインスタンスを初期化します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    dictionary As IDictionary(Of TKey, TValue),
 _
    comparer As IComparer(Of TKey) _
)
Dim dictionary As IDictionary(Of
 TKey, TValue)
Dim comparer As IComparer(Of
 TKey)

Dim instance As New SortedDictionary(Of
 TKey, TValue)(dictionary, comparer)
public SortedDictionary (
    IDictionary<TKey,TValue> dictionary,
    IComparer<TKey> comparer
)
public:
SortedDictionary (
    IDictionary<TKey, TValue>^ dictionary, 
    IComparer<TKey>^ comparer
)
public SortedDictionary (
    IDictionary<TKey,TValue> dictionary, 
    IComparer<TKey> comparer
)
public function SortedDictionary (
    dictionary : IDictionary<TKey,TValue>, 
    comparer : IComparer<TKey>
)

パラメータ

dictionary

新しい SortedDictionary に要素がコピーされた IDictionary。

comparer

キーの比較時に使用する IComparer 実装。キーの型の既定の Comparer を使用する場合は null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

dictionary が null 参照 (Visual Basic では Nothing) です。

ArgumentException

dictionary に、1 つ以上の重複するキーが格納されています。

解説解説

SortedDictionary 内のすべてのキーは、指定した比較演算子に従って一意である必要があります。したがって、元の dictionary 内のすべてのキーも、指定した比較演算子に従って一意である必要があります。

SortedDictionary は、キーの比較を実行するために比較演算子の実装を必要とします。comparer が null 参照 (Visual Basic では Nothing) の場合、このコンストラクタは、既定のジェネリック等値比較演算子である Comparer.Default を使用します。型 TKey が System.IComparable ジェネリック インターフェイスを実装している場合は、既定の比較演算子でその実装が使用されます。

このコンストラクタは O(n log n) 操作です。ここで、n は dictionary 内の要素数です。

使用例使用例

Dictionary を SortedDictionary(ジェネリック IDictionary,ジェネリック IComparer) コンストラクタに渡すことにより、SortedDictionary を使用して、大文字小文字を区別しない Dictionary 内の情報の、大文字小文字を区別せずに並べ替えられたコピーを作成する方法を次のコード例に示します。この例では、現在のカルチャの大文字小文字を区別しない比較演算子が使用されます。

Imports System
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main()
 

        ' Create a new Dictionary of strings, with string keys and
        ' a case-insensitive equality comparer for the current 
        ' culture.
        Dim openWith As New
 Dictionary(Of String, String)(
 _
            StringComparer.CurrentCultureIgnoreCase)
        
        ' Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe")
        openWith.Add("Bmp", "paint.exe")
        openWith.Add("DIB", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' List the contents of the Dictionary.
        Console.WriteLine()
        For Each kvp As
 KeyValuePair(Of String, String)
 In openWith
            Console.WriteLine("Key = {0}, Value = {1}",
 _
                kvp.Key, kvp.Value)
        Next kvp

        ' Create a SortedDictionary of strings with string keys and
 a 
        ' case-insensitive equality comparer for the current culture
,
        ' and initialize it with the contents of the Dictionary.
        Dim copy As New
 SortedDictionary(Of String, String)(openWith,
 _
            StringComparer.CurrentCultureIgnoreCase)

        ' List the sorted contents of the copy.
        Console.WriteLine()
        For Each kvp As
 KeyValuePair(Of String, String)
 In copy
            Console.WriteLine("Key = {0}, Value = {1}",
 _
                kvp.Key, kvp.Value)
        Next kvp

    End Sub

End Class

' This code example produces the following output:
'
'Key = txt, Value = notepad.exe
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new Dictionary of strings, with string keys and
        // a case-insensitive equality comparer for the current 
        // culture.
        Dictionary<string, string> openWith
 = 
            new Dictionary<string, string>
                (StringComparer.CurrentCultureIgnoreCase);
        
        // Add some elements to the dictionary. 
        openWith.Add("txt", "notepad.exe");
        openWith.Add("Bmp", "paint.exe");
        openWith.Add("DIB", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
        
        // List the contents of the Dictionary.
        Console.WriteLine();
        foreach( KeyValuePair<string, string>
 kvp in openWith)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, 
                kvp.Value);
        }

        // Create a SortedDictionary of strings with string keys and
 a 
        // case-insensitive equality comparer for the current culture
,
        // and initialize it with the contents of the Dictionary.
        SortedDictionary<string, string>
 copy = 
                    new SortedDictionary<string,
 string>(openWith, 
                        StringComparer.CurrentCultureIgnoreCase);

        // List the sorted contents of the copy.
        Console.WriteLine();
        foreach( KeyValuePair<string, string>
 kvp in copy )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, 
                kvp.Value);
        }
    }
}

/* This code example produces the following output:

Key = txt, Value = notepad.exe
Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe

Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedDictionary ジェネリック クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

キーで並べ替えられた、キー/値ペアのコレクションを表します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

<SerializableAttribute> _
Public Class SortedDictionary(Of
 TKey, TValue)
    Implements IDictionary(Of TKey, TValue),
 ICollection(Of KeyValuePair(Of TKey, TValue)),
 _
    IEnumerable(Of KeyValuePair(Of TKey, TValue)),
 IDictionary, ICollection, _
    IEnumerable
Dim instance As SortedDictionary(Of
 TKey, TValue)
[SerializableAttribute] 
public class SortedDictionary<TKey,TValue>
 : IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>,
 
    IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection,
 IEnumerable
[SerializableAttribute] 
generic<typename TKey, typename TValue>
public ref class SortedDictionary : IDictionary<TKey,
 TValue>, ICollection<KeyValuePair<TKey, TValue>>, 
    IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection,
 IEnumerable
J# では、ジェネリックな型およびメソッドは使用できますが、新規に宣言することはできません。
JScript では、ジェネリックな型およびメソッドは使用できません。

型パラメータ

TKey

ディクショナリ内のキーの型。

TValue

ディクショナリ内の値の型。

解説解説

SortedDictionary ジェネリック クラスは、O(log n) 取得を使用するバイナリ サーチ ツリーです。n は、ディクショナリ内の要素数を示します。この点で、これは SortedList ジェネリック クラスに似ています。この 2 つのクラスには、同じようなオブジェクト モデルがあり、どちらも O(log n) 取得アルゴリズムを備えています。この 2 つのクラスの違いは、メモリの使用方法と、挿入および削除の速度です。

各キー/値ペアは、KeyValuePair 構造体として、または非ジェネリックの IDictionary インターフェイスを使って DictionaryEntry として取得できます。

キーは、SortedDictionary でキーとして使用されている間は不変であることが必要です。SortedDictionary 内のすべてのキーは一意である必要があります。キーを null 参照 (Visual Basic では Nothing) にすることはできませんが、値の型 TValue が参照型である場合、値を null にすることはできます。

SortedDictionary は、キーの比較を実行するために比較演算子の実装を必要とします。comparer パラメータを受け取るコンストラクタを使用して、IComparer ジェネリック インターフェイスの実装を指定できます。実装を指定しない場合は、既定のジェネリック比較演算子である Comparer.Default が使用されます。型 TKey が System.IComparable ジェネリック インターフェイスを実装している場合は、既定の比較演算子でその実装が使用されます。

C# 言語の foreach ステートメント (C++ の場合は for each、Visual Basic の場合は For Each) は、コレクション内の各要素の型を必要とします。SortedDictionary の各要素はキー/値ペアであるため、要素の型は、キーの型や値の型にはなりません。その代わり、要素の型は KeyValuePair になります。C#、C++、および Visual Basic の構文を次のコードに示します。

foreach (KeyValuePair<int, string>
 kvp in myDictionary) {...}
for each (KeyValuePair<int, String^>
 kvp in myDictionary) {...}
For Each kvp As KeyValuePair(Of
 Integer, String) In myDictionary
    ...
Next myKVP

foreach ステートメントは、列挙子のラッパーです。これは、コレクションからの読み取りだけを許可し、コレクションへの書き込みはできません。

使用例使用例

文字列キーを含む文字列の空の SortedDictionary を作成し、Add メソッドを使用していくつかの要素を追加するコード例を次に示します。この例では、重複するキーを追加しようとすると、Add メソッドが ArgumentException をスローすることを示します。

この例では、Item プロパティ (C# ではインデクサ) を使用して値を取得し、要求されたキーが存在しないときに KeyNotFoundException がスローされる例を示し、またキーに関連付けられた値を置き換えることができることも示します。

この例では、プログラムがディクショナリにないキー値を頻繁に試行する必要がある場合に、より効率的に値を取得する方法として TryGetValue メソッドを使用する方法、および ContainsKey メソッドを使用して、Add メソッドを呼び出す前に、キーが存在するかどうかをテストする方法を示します。

この例では、ディクショナリのキーと値を列挙する方法、および Keys プロパティと Values プロパティを使用してキーと値のみを列挙する方法を示します。

最後に、Remove メソッドの使用例を示します。

Imports System
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main()
 

        ' Create a new dictionary of strings, with string keys.
        '
        Dim openWith As New
 Dictionary(Of String, String)
        
        ' Add some elements to the dictionary. There are no 
        ' duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' The Add method throws an exception if the new key is 
        ' already in the dictionary.
        Try
            openWith.Add("txt", "winword.exe")
        Catch 
            Console.WriteLine("An element with Key = ""txt""
 already exists.")
        End Try

        ' The Item property is the default property, so you 
        ' can omit its name when accessing elements. 
        Console.WriteLine("For key = ""rtf"",
 value = {0}.", _
            openWith("rtf"))
        
        ' The default Item property can be used to change the value
        ' associated with a key.
        openWith("rtf") = "winword.exe"
        Console.WriteLine("For key = ""rtf"",
 value = {0}.", _
            openWith("rtf"))
        
        ' If a key does not exist, setting the default Item property
        ' for that key adds a new key/value pair.
        openWith("doc") = "winword.exe"

        ' The default Item property throws an exception if the requested
        ' key is not in the dictionary.
        Try
            Console.WriteLine("For key = ""tif"",
 value = {0}.", _
                openWith("tif"))
        Catch 
            Console.WriteLine("Key = ""tif""
 is not found.")
        End Try

        ' When a program often has to try keys that turn out not to
        ' be in the dictionary, TryGetValue can be a more efficient
 
        ' way to retrieve values.
        Dim value As String
 = ""
        If openWith.TryGetValue("tif",
 value) Then
            Console.WriteLine("For key = ""tif"",
 value = {0}.", value)
        Else
            Console.WriteLine("Key = ""tif""
 is not found.")
        End If

        ' ContainsKey can be used to test keys before inserting 
        ' them.
        If Not openWith.ContainsKey("ht")
 Then
            openWith.Add("ht", "hypertrm.exe")
            Console.WriteLine("Value added for key = ""ht"":
 {0}", _
                openWith("ht"))
        End If

        ' When you use foreach to enumerate dictionary elements,
        ' the elements are retrieved as KeyValuePair objects.
        Console.WriteLine()
        For Each kvp As
 KeyValuePair(Of String, String)
 In openWith
            Console.WriteLine("Key = {0}, Value = {1}",
 _
                kvp.Key, kvp.Value)
        Next kvp

        ' To get the values alone, use the Values property.
        Dim valueColl As _
            Dictionary(Of String, String).ValueCollection
 = _
            openWith.Values
        
        ' The elements of the ValueCollection are strongly typed
        ' with the type that was specified for dictionary values.
        Console.WriteLine()
        For Each s As String
 In  valueColl
            Console.WriteLine("Value = {0}", s)
        Next s

        ' To get the keys alone, use the Keys property.
        Dim keyColl As _
            Dictionary(Of String, String).KeyCollection
 = _
            openWith.Keys
        
        ' The elements of the KeyCollection are strongly typed
        ' with the type that was specified for dictionary keys.
        Console.WriteLine()
        For Each s As String
 In  keyColl
            Console.WriteLine("Key = {0}", s)
        Next s

        ' Use the Remove method to remove a key/value pair.
        Console.WriteLine(vbLf + "Remove(""doc"")")
        openWith.Remove("doc")
        
        If Not openWith.ContainsKey("doc")
 Then
            Console.WriteLine("Key ""doc""
 is not found.")
        End If

    End Sub

End Class

' This code example produces the following output:
'
'An element with Key = "txt" already exists.
'For key = "rtf", value = wordpad.exe.
'For key = "rtf", value = winword.exe.
'Key = "tif" is not found.
'Key = "tif" is not found.
'Value added for key = "ht": hypertrm.exe
'
'Key = txt, Value = notepad.exe
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = winword.exe
'Key = doc, Value = winword.exe
'Key = ht, Value = hypertrm.exe
'
'Value = notepad.exe
'Value = paint.exe
'Value = paint.exe
'Value = winword.exe
'Value = winword.exe
'Value = hypertrm.exe
'
'Key = txt
'Key = bmp
'Key = dib
'Key = rtf
'Key = doc
'Key = ht
'
'Remove("doc")
'Key "doc" is not found.
' 
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys.
        //
        Dictionary<string, string> openWith
 = 
            new Dictionary<string, string>();

        // Add some elements to the dictionary. There are no 
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // The Add method throws an exception if the new key is 
        // already in the dictionary.
        try
        {
            openWith.Add("txt", "winword.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("An element with Key = \"txt\" already
 exists.");
        }

        // The Item property is another name for the indexer, so you
 
        // can omit its name when accessing elements. 
        Console.WriteLine("For key = \"rtf\", value = {0}.",
 
            openWith["rtf"]);

        // The indexer can be used to change the value associated
        // with a key.
        openWith["rtf"] = "winword.exe";
        Console.WriteLine("For key = \"rtf\", value = {0}.",
 
            openWith["rtf"]);

        // If a key does not exist, setting the indexer for that key
        // adds a new key/value pair.
        openWith["doc"] = "winword.exe";

        // The indexer throws an exception if the requested key is
        // not in the dictionary.
        try
        {
            Console.WriteLine("For key = \"tif\", value = {0}.",
 
                openWith["tif"]);
        }
        catch (KeyNotFoundException)
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }

        // When a program often has to try keys that turn out not to
        // be in the dictionary, TryGetValue can be a more efficient
 
        // way to retrieve values.
        string value = "";
        if (openWith.TryGetValue("tif", out value))
        {
            Console.WriteLine("For key = \"tif\", value = {0}.",
 value);
        }
        else
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }

        // ContainsKey can be used to test keys before inserting 
        // them.
        if (!openWith.ContainsKey("ht"))
        {
            openWith.Add("ht", "hypertrm.exe");
            Console.WriteLine("Value added for key = \"ht\":
 {0}", 
                openWith["ht"]);
        }

        // When you use foreach to enumerate dictionary elements,
        // the elements are retrieved as KeyValuePair objects.
        Console.WriteLine();
        foreach( KeyValuePair<string, string>
 kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", 
                kvp.Key, kvp.Value);
        }

        // To get the values alone, use the Values property.
        Dictionary<string, string>.ValueCollection
 valueColl =
            openWith.Values;

        // The elements of the ValueCollection are strongly typed
        // with the type that was specified for dictionary values.
        Console.WriteLine();
        foreach( string s in
 valueColl )
        {
            Console.WriteLine("Value = {0}", s);
        }

        // To get the keys alone, use the Keys property.
        Dictionary<string, string>.KeyCollection
 keyColl =
            openWith.Keys;

        // The elements of the KeyCollection are strongly typed
        // with the type that was specified for dictionary keys.
        Console.WriteLine();
        foreach( string s in
 keyColl )
        {
            Console.WriteLine("Key = {0}", s);
        }

        // Use the Remove method to remove a key/value pair.
        Console.WriteLine("\nRemove(\"doc\")");
        openWith.Remove("doc");

        if (!openWith.ContainsKey("doc"))
        {
            Console.WriteLine("Key \"doc\" is not found.");
        }
    }
}

/* This code example produces the following output:

An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe

Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe

Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Value = hypertrm.exe

Key = txt
Key = bmp
Key = dib
Key = rtf
Key = doc
Key = ht

Remove("doc")
Key "doc" is not found.
 */
継承階層継承階層
System.Object
  System.Collections.Generic.SortedDictionary
スレッド セーフスレッド セーフ

この型の public static (Visual Basic では Shared) メンバは、スレッド セーフです。すべてのインスタンス メンバがスレッド セーフになるかどうかは保証されていません。

コレクションが変更されない限り、SortedDictionary では、複数の読み込み操作が同時に発生しても問題ありません。ただし、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションをロックすることにより、列挙処理でのスレッド セーフを確保できます。コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortedDictionary メンバ
System.Collections.Generic 名前空間
SortedList クラス
Dictionary ジェネリック クラス

SortedDictionary プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Comparer SortedDictionary の要素の順序付けに使用する IComparer を取得します。
パブリック プロパティ Count SortedDictionary に格納されているキー/値ペアの数を取得します。
パブリック プロパティ Item 指定したキーに関連付けられている値を取得または設定します。
パブリック プロパティ Keys SortedDictionary 内のキーを格納しているコレクションを取得します。
パブリック プロパティ Values SortedDictionary 内の値を格納しているコレクションを取得します。
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.IsReadOnly ICollection が読み取り専用かどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.Generic.IDictionary<TKey,TValue>.Keys IDictionary のキーを保持している ICollection を取得します。
インターフェイスの明示的な実装 System.Collections.Generic.IDictionary<TKey,TValue>.Values IDictionary 内の値を格納している ICollection を取得します。
インターフェイスの明示的な実装 System.Collections.ICollection.IsSynchronized ICollection へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.ICollection.SyncRoot ICollection へのアクセスを同期するために使用できるオブジェクトを取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.IsFixedSize IDictionary が固定サイズかどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.IsReadOnly IDictionary が読み取り専用かどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Item 指定したキーを持つ要素を取得または設定します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Keys IDictionary のキーを保持している ICollection を取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Values IDictionary 内の値を格納している ICollection を取得します。
参照参照

関連項目

SortedDictionary ジェネリック クラス
System.Collections.Generic 名前空間
SortedList クラス
Dictionary ジェネリック クラス

SortedDictionary メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Add 指定したキーおよび値を持つ要素を SortedDictionary に追加します。
パブリック メソッド Clear SortedDictionary からすべての要素を削除します。
パブリック メソッド ContainsKey 指定したキーの要素が SortedDictionary に格納されているかどうかを確認します。
パブリック メソッド ContainsValue 指定した値の要素が SortedDictionary に格納されているかどうかを確認します。
パブリック メソッド CopyTo 指定したインデックスを開始位置として、指定した KeyValuePair 構造体の配列に SortedDictionary 要素をコピーします。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetEnumerator SortedDictionary を反復処理する列挙子を返します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド Remove 指定したキーを持つ要素を SortedDictionary から削除します。
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
パブリック メソッド TryGetValue 指定したキーに関連付けられている値を取得します。
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Add ICollection に項目を追加します。
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Contains ICollection に特定のキーと値が格納されているかどうかを判断します。
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Remove ICollection 内で最初に見つかった指定の要素を削除します。
インターフェイスの明示的な実装 System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>.GetEnumerator コレクションを反復処理する列挙子を返します。
インターフェイスの明示的な実装 System.Collections.ICollection.CopyTo 指定した配列インデックスを開始位置として、配列に ICollection の要素をコピーします。
インターフェイスの明示的な実装 System.Collections.IDictionary.Add 指定したキーおよび値を持つ要素を IDictionary に追加します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Contains 指定したキーの要素が IDictionary に格納されているかどうかを確認します。
インターフェイスの明示的な実装 System.Collections.IDictionary.GetEnumerator IDictionary の IDictionaryEnumerator を返します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Remove 指定したキーを持つ要素を IDictionary から削除します。
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator コレクションを反復処理する列挙子を返します。
参照参照

関連項目

SortedDictionary ジェネリック クラス
System.Collections.Generic 名前空間
SortedList クラス
Dictionary ジェネリック クラス

SortedDictionary メンバ

キーで並べ替えられた、キー/値ペアのコレクションを表します。

SortedDictionary ジェネリック型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Comparer SortedDictionary の要素の順序付けに使用する IComparer を取得します。
パブリック プロパティ Count SortedDictionary に格納されているキー/値ペアの数を取得します。
パブリック プロパティ Item 指定したキーに関連付けられている値を取得または設定します。
パブリック プロパティ Keys SortedDictionary 内のキーを格納しているコレクションを取得します。
パブリック プロパティ Values SortedDictionary 内の値を格納しているコレクションを取得します。
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Add 指定したキーおよび値を持つ要素を SortedDictionary に追加します。
パブリック メソッド Clear SortedDictionary からすべての要素を削除します。
パブリック メソッド ContainsKey 指定したキーの要素が SortedDictionary に格納されているかどうかを確認します。
パブリック メソッド ContainsValue 指定した値の要素が SortedDictionary に格納されているかどうかを確認します。
パブリック メソッド CopyTo 指定したインデックスを開始位置として、指定した KeyValuePair 構造体の配列に SortedDictionary 要素をコピーします。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetEnumerator SortedDictionary を反復処理する列挙子を返します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド Remove 指定したキーを持つ要素を SortedDictionary から削除します。
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
パブリック メソッド TryGetValue 指定したキーに関連付けられている値を取得します。
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイスの実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Add ICollection に項目を追加します。
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Contains ICollection に特定のキーと値が格納されているかどうかを判断します。
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Remove ICollection 内で最初に見つかった指定の要素を削除します。
インターフェイスの明示的な実装 System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>.GetEnumerator コレクションを反復処理する列挙子を返します。
インターフェイスの明示的な実装 System.Collections.ICollection.CopyTo 指定した配列インデックスを開始位置として、配列に ICollection の要素をコピーします。
インターフェイスの明示的な実装 System.Collections.IDictionary.Add 指定したキーおよび値を持つ要素を IDictionary に追加します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Contains 指定したキーの要素が IDictionary に格納されているかどうかを確認します。
インターフェイスの明示的な実装 System.Collections.IDictionary.GetEnumerator IDictionary の IDictionaryEnumerator を返します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Remove 指定したキーを持つ要素を IDictionary から削除します。
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator コレクションを反復処理する列挙子を返します。
インターフェイスの明示的な実装 System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.IsReadOnly ICollection が読み取り専用かどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.Generic.IDictionary<TKey,TValue>.Keys IDictionary のキーを保持している ICollection を取得します。
インターフェイスの明示的な実装 System.Collections.Generic.IDictionary<TKey,TValue>.Values IDictionary 内の値を格納している ICollection を取得します。
インターフェイスの明示的な実装 System.Collections.ICollection.IsSynchronized ICollection へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.ICollection.SyncRoot ICollection へのアクセスを同期するために使用できるオブジェクトを取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.IsFixedSize IDictionary が固定サイズかどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.IsReadOnly IDictionary が読み取り専用かどうかを示す値を取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Item 指定したキーを持つ要素を取得または設定します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Keys IDictionary のキーを保持している ICollection を取得します。
インターフェイスの明示的な実装 System.Collections.IDictionary.Values IDictionary 内の値を格納している ICollection を取得します。
参照参照

関連項目

SortedDictionary ジェネリック クラス
System.Collections.Generic 名前空間
SortedList クラス
Dictionary ジェネリック クラス



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「SortedDictionary」の関連用語

SortedDictionaryのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SortedDictionaryのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS