SortedList コンストラクタ (IDictionary)
アセンブリ: mscorlib (mscorlib.dll 内)



各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイスを実装する必要があります。要素は、SortedList に追加された各キーの IComparable 実装に基づいて並べ替えられます。
Hashtable は、このコンストラクタに渡すことができる IDictionary 実装の一例です。新しい SortedList には、Hashtable で並べ替えられたキーと値のコピーが格納されます。
SortedList の容量は、SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

異なる SortedList コンストラクタを使用してコレクションを作成し、各コレクションの動作の違いを示すコード例を次に示します。
Imports System Imports System.Collections Imports System.Globalization Public Class SamplesSortedList Public Shared Sub Main() ' Create the dictionary. Dim myHT As New Hashtable() myHT.Add("FIRST", "Hello") myHT.Add("SECOND", "World") myHT.Add("THIRD", "!") ' Create a SortedList using the default comparer. Dim mySL1 As New SortedList(myHT) Console.WriteLine("mySL1 (default):") Try mySL1.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL1) ' Create a SortedList using the specified case-insensitive comparer. Dim mySL2 As New SortedList(myHT, New CaseInsensitiveComparer()) Console.WriteLine("mySL2 (case-insensitive comparer):") Try mySL2.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL2) ' Create a SortedList using the specified CaseInsensitiveComparer , ' which is based on the Turkish culture (tr-TR), where "I" is not ' the uppercase version of "i". Dim myCul As New CultureInfo("tr-TR") Dim mySL3 As New SortedList(myHT, New CaseInsensitiveComparer(myCul)) Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):") Try mySL3.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL3) ' Create a SortedList using the ' StringComparer.InvariantCultureIgnoreCase value. Dim mySL4 As New SortedList(myHT, StringComparer.InvariantCultureIgnoreCase) Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):") Try mySL4.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL4) End Sub 'Main Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList) Console.WriteLine(" -KEY- -VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(" {0,-6}: {1}", _ myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesSortedList 'This code produces the following output. Results vary depending on the system's culture settings. ' 'mySL1 (default): ' -KEY- -VALUE- ' first : Ola! ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL2 (case-insensitive comparer): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL3 (case-insensitive comparer, Turkish culture): ' -KEY- -VALUE- ' FIRST : Hello ' first : Ola! ' SECOND: World ' THIRD : ! ' 'mySL4 (InvariantCultureIgnoreCase): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : !
using System; using System.Collections; using System.Globalization; public class SamplesSortedList { public static void Main() { // Create the dictionary. Hashtable myHT = new Hashtable(); myHT.Add("FIRST", "Hello"); myHT.Add("SECOND", "World"); myHT.Add("THIRD", "!"); // Create a SortedList using the default comparer. SortedList mySL1 = new SortedList(myHT); Console.WriteLine("mySL1 (default):"); try { mySL1.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL1); // Create a SortedList using the specified case-insensitive comparer. SortedList mySL2 = new SortedList(myHT, new CaseInsensitiveComparer()); Console.WriteLine("mySL2 (case-insensitive comparer):"); try { mySL2.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL2); // Create a SortedList using the specified CaseInsensitiveComparer , // which is based on the Turkish culture (tr-TR), where "I" is not // the uppercase version of "i". CultureInfo myCul = new CultureInfo("tr-TR"); SortedList mySL3 = new SortedList(myHT, new CaseInsensitiveComparer(myCul)); Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):"); try { mySL3.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL3); // Create a SortedList using the // StringComparer.InvariantCultureIgnoreCase value. SortedList mySL4 = new SortedList( myHT, StringComparer.InvariantCultureIgnoreCase); Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):"); try { mySL4.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL4); } public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine(" -KEY- -VALUE-"); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(" {0,-6}: {1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } } /* This code produces the following output. Results vary depending on the system's culture settings. mySL1 (default): -KEY- -VALUE- first : Ola! FIRST : Hello SECOND: World THIRD : ! mySL2 (case-insensitive comparer): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! mySL3 (case-insensitive comparer, Turkish culture): -KEY- -VALUE- FIRST : Hello first : Ola! SECOND: World THIRD : ! mySL4 (InvariantCultureIgnoreCase): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList コンストラクタ (Int32, ジェネリック IComparer)
アセンブリ: System (system.dll 内)

Dim capacity As Integer Dim comparer As IComparer(Of TKey) Dim instance As New SortedList(Of TKey, TValue)(capacity, comparer)


SortedList 内のすべてのキーは指定した比較演算子に従って一意である必要があります。
SortedList の容量は、サイズ変更前に SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。
容量を減らすには、TrimExcess を呼び出すか、Capacity プロパティを明示的に設定します。容量を減らすと、メモリの再割り当てが行われ、SortedList 内のすべての要素がコピーされます。

初期量 5 で、現在のカルチャの大文字小文字を区別しない比較演算子を使って、並べ替えられたリストを作成するコード例を次に示します。この例では、小文字のキーを含む要素と大文字のキーを含む要素を合わせて 4 つの要素を追加します。次に、既存のキーとの違いが大文字と小文字の違いだけであるキーを含む要素の追加を試行し、結果の例外をキャッチし、エラー メッセージを表示します。最後に、大文字小文字を区別しない並べ替え順で要素を表示します。
Imports System Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new sorted list of strings, with string keys, an ' initial capacity of 5, and a case-insensitive comparer. Dim openWith As New SortedList(Of String, String)(5, _ StringComparer.CurrentCultureIgnoreCase) ' Add 4 elements to the list. 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 sorted list.") End Try ' List the contents of the sorted list. 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 sorted list. ' '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 sorted list of strings, with string keys, an // initial capacity of 5, and a case-insensitive comparer. SortedList<string, string> openWith = new SortedList<string, string>(5, StringComparer.CurrentCultureIgnoreCase); // Add 4 elements to the list. 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 sorted list."); } // List the contents of the sorted list. 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 sorted list. Key = bmp, Value = paint.exe Key = DIB, Value = paint.exe Key = rtf, Value = wordpad.exe Key = txt, Value = notepad.exe */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


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

名前 | 説明 |
---|---|
SortedList () | 空で、既定の初期量を備え、SortedList に追加された各キーによって実装されている IComparable インターフェイスに従って並べ替えられた、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (IComparer) | 空で、既定の初期量を備え、指定した IComparer インターフェイスに従って並べ替えられた、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (IDictionary) | 指定したディクショナリからコピーした要素を格納し、コピーした要素の数と同じ初期量を備え、各キーによって実装されている IComparable インターフェイスに従って並べ替えられた、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (Int32) | 空で、既定の初期量を備え、SortedList に追加された各キーによって実装されている IComparable インターフェイスに従って並べ替えられた、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (IComparer, Int32) | 空で、指定した初期量を備え、指定した IComparer インターフェイスに従って並べ替えられた、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (IDictionary, IComparer) | 指定したディクショナリからコピーした要素を格納し、コピーした要素の数と同じ初期量を備え、指定した IComparer インターフェイスに従って並べ替えられた、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |

SortedList コンストラクタ (Int32)
アセンブリ: mscorlib (mscorlib.dll 内)



各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイスを実装する必要があります。要素は、SortedList に追加された各キーの IComparable 実装に基づいて並べ替えられます。
SortedList の容量は、SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

異なる SortedList コンストラクタを使用してコレクションを作成し、各コレクションの動作の違いを示すコード例を次に示します。
Imports System Imports System.Collections Imports System.Globalization Public Class SamplesSortedList Public Shared Sub Main() ' Create a SortedList using the default comparer. Dim mySL1 As New SortedList( 3 ) Console.WriteLine("mySL1 (default):") mySL1.Add("FIRST", "Hello") mySL1.Add("SECOND", "World") mySL1.Add("THIRD", "!") Try mySL1.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL1) ' Create a SortedList using the specified case-insensitive comparer. Dim mySL2 As New SortedList(New CaseInsensitiveComparer(), 3) Console.WriteLine("mySL2 (case-insensitive comparer):") mySL2.Add("FIRST", "Hello") mySL2.Add("SECOND", "World") mySL2.Add("THIRD", "!") Try mySL2.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL2) ' Create a SortedList using the specified CaseInsensitiveComparer , ' which is based on the Turkish culture (tr-TR), where "I" is not ' the uppercase version of "i". Dim myCul As New CultureInfo("tr-TR") Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul), 3) Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):") mySL3.Add("FIRST", "Hello") mySL3.Add("SECOND", "World") mySL3.Add("THIRD", "!") Try mySL3.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL3) ' Create a SortedList using the ' StringComparer.InvariantCultureIgnoreCase value. Dim mySL4 As New SortedList( _ StringComparer.InvariantCultureIgnoreCase, 3) Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):") mySL4.Add("FIRST", "Hello") mySL4.Add("SECOND", "World") mySL4.Add("THIRD", "!") Try mySL4.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL4) End Sub 'Main Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList) Console.WriteLine(" -KEY- -VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(" {0,-6}: {1}", _ myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesSortedList 'This code produces the following output. Results vary depending on the system's culture settings. ' 'mySL1 (default): ' -KEY- -VALUE- ' first : Ola! ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL2 (case-insensitive comparer): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL3 (case-insensitive comparer, Turkish culture): ' -KEY- -VALUE- ' FIRST : Hello ' first : Ola! ' SECOND: World ' THIRD : ! ' 'mySL4 (InvariantCultureIgnoreCase): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : !
using System; using System.Collections; using System.Globalization; public class SamplesSortedList { public static void Main() { // Create a SortedList using the default comparer. SortedList mySL1 = new SortedList( 3 ); Console.WriteLine("mySL1 (default):"); mySL1.Add("FIRST", "Hello"); mySL1.Add("SECOND", "World"); mySL1.Add("THIRD", "!"); try { mySL1.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL1); // Create a SortedList using the specified case-insensitive comparer. SortedList mySL2 = new SortedList(new CaseInsensitiveComparer(), 3); Console.WriteLine("mySL2 (case-insensitive comparer):"); mySL2.Add("FIRST", "Hello"); mySL2.Add("SECOND", "World"); mySL2.Add("THIRD", "!"); try { mySL2.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL2); // Create a SortedList using the specified CaseInsensitiveComparer , // which is based on the Turkish culture (tr-TR), where "I" is not // the uppercase version of "i". CultureInfo myCul = new CultureInfo("tr-TR"); SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul), 3); Console.WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):"); mySL3.Add("FIRST", "Hello"); mySL3.Add("SECOND", "World"); mySL3.Add("THIRD", "!"); try { mySL3.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL3); // Create a SortedList using the // StringComparer.InvariantCultureIgnoreCase value. SortedList mySL4 = new SortedList( StringComparer.InvariantCultureIgnoreCase, 3); Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):"); mySL4.Add("FIRST", "Hello"); mySL4.Add("SECOND", "World"); mySL4.Add("THIRD", "!"); try { mySL4.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL4); } public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine(" -KEY- -VALUE-"); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(" {0,-6}: {1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } } /* This code produces the following output. Results vary depending on the system's culture settings. mySL1 (default): -KEY- -VALUE- first : Ola! FIRST : Hello SECOND: World THIRD : ! mySL2 (case-insensitive comparer): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! mySL3 (case-insensitive comparer, Turkish culture): -KEY- -VALUE- FIRST : Hello first : Ola! SECOND: World THIRD : ! mySL4 (InvariantCultureIgnoreCase): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList コンストラクタ (IComparer, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim comparer As IComparer Dim capacity As Integer Dim instance As New SortedList(comparer, capacity)


要素は、指定した IComparer 実装に基づいて並べ替えられます。comparer が null 参照 (Visual Basic では Nothing) の場合、各キーの IComparable 実装が使用されます。したがって、各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイスを実装する必要があります。
SortedList の容量は、SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

異なる SortedList コンストラクタを使用してコレクションを作成し、各コレクションの動作の違いを示すコード例を次に示します。
Imports System Imports System.Collections Imports System.Globalization Public Class SamplesSortedList Public Shared Sub Main() ' Create a SortedList using the default comparer. Dim mySL1 As New SortedList( 3 ) Console.WriteLine("mySL1 (default):") mySL1.Add("FIRST", "Hello") mySL1.Add("SECOND", "World") mySL1.Add("THIRD", "!") Try mySL1.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL1) ' Create a SortedList using the specified case-insensitive comparer. Dim mySL2 As New SortedList(New CaseInsensitiveComparer(), 3) Console.WriteLine("mySL2 (case-insensitive comparer):") mySL2.Add("FIRST", "Hello") mySL2.Add("SECOND", "World") mySL2.Add("THIRD", "!") Try mySL2.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL2) ' Create a SortedList using the specified CaseInsensitiveComparer , ' which is based on the Turkish culture (tr-TR), where "I" is not ' the uppercase version of "i". Dim myCul As New CultureInfo("tr-TR") Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul), 3) Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):") mySL3.Add("FIRST", "Hello") mySL3.Add("SECOND", "World") mySL3.Add("THIRD", "!") Try mySL3.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL3) ' Create a SortedList using the ' StringComparer.InvariantCultureIgnoreCase value. Dim mySL4 As New SortedList( _ StringComparer.InvariantCultureIgnoreCase, 3) Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):") mySL4.Add("FIRST", "Hello") mySL4.Add("SECOND", "World") mySL4.Add("THIRD", "!") Try mySL4.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL4) End Sub 'Main Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList) Console.WriteLine(" -KEY- -VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(" {0,-6}: {1}", _ myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesSortedList 'This code produces the following output. Results vary depending on the system's culture settings. ' 'mySL1 (default): ' -KEY- -VALUE- ' first : Ola! ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL2 (case-insensitive comparer): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL3 (case-insensitive comparer, Turkish culture): ' -KEY- -VALUE- ' FIRST : Hello ' first : Ola! ' SECOND: World ' THIRD : ! ' 'mySL4 (InvariantCultureIgnoreCase): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : !
using System; using System.Collections; using System.Globalization; public class SamplesSortedList { public static void Main() { // Create a SortedList using the default comparer. SortedList mySL1 = new SortedList( 3 ); Console.WriteLine("mySL1 (default):"); mySL1.Add("FIRST", "Hello"); mySL1.Add("SECOND", "World"); mySL1.Add("THIRD", "!"); try { mySL1.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL1); // Create a SortedList using the specified case-insensitive comparer. SortedList mySL2 = new SortedList(new CaseInsensitiveComparer(), 3); Console.WriteLine("mySL2 (case-insensitive comparer):"); mySL2.Add("FIRST", "Hello"); mySL2.Add("SECOND", "World"); mySL2.Add("THIRD", "!"); try { mySL2.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL2); // Create a SortedList using the specified CaseInsensitiveComparer , // which is based on the Turkish culture (tr-TR), where "I" is not // the uppercase version of "i". CultureInfo myCul = new CultureInfo("tr-TR"); SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul), 3); Console.WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):"); mySL3.Add("FIRST", "Hello"); mySL3.Add("SECOND", "World"); mySL3.Add("THIRD", "!"); try { mySL3.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL3); // Create a SortedList using the // StringComparer.InvariantCultureIgnoreCase value. SortedList mySL4 = new SortedList( StringComparer.InvariantCultureIgnoreCase, 3); Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):"); mySL4.Add("FIRST", "Hello"); mySL4.Add("SECOND", "World"); mySL4.Add("THIRD", "!"); try { mySL4.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL4); } public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine(" -KEY- -VALUE-"); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(" {0,-6}: {1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } } /* This code produces the following output. Results vary depending on the system's culture settings. mySL1 (default): -KEY- -VALUE- first : Ola! FIRST : Hello SECOND: World THIRD : ! mySL2 (case-insensitive comparer): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! mySL3 (case-insensitive comparer, Turkish culture): -KEY- -VALUE- FIRST : Hello first : Ola! SECOND: World THIRD : ! mySL4 (InvariantCultureIgnoreCase): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList コンストラクタ (IDictionary, IComparer)
アセンブリ: mscorlib (mscorlib.dll 内)



要素は、指定した IComparer 実装に基づいて並べ替えられます。comparer が null 参照 (Visual Basic では Nothing) の場合、各キーの IComparable 実装が使用されます。したがって、各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイスを実装する必要があります。
Hashtable は、このコンストラクタに渡すことができる IDictionary 実装の一例です。新しい SortedList には、Hashtable で並べ替えられたキーと値のコピーが格納されます。
SortedList の容量は、SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

異なる SortedList コンストラクタを使用してコレクションを作成し、各コレクションの動作の違いを示すコード例を次に示します。
Imports System Imports System.Collections Imports System.Globalization Public Class SamplesSortedList Public Shared Sub Main() ' Create the dictionary. Dim myHT As New Hashtable() myHT.Add("FIRST", "Hello") myHT.Add("SECOND", "World") myHT.Add("THIRD", "!") ' Create a SortedList using the default comparer. Dim mySL1 As New SortedList(myHT) Console.WriteLine("mySL1 (default):") Try mySL1.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL1) ' Create a SortedList using the specified case-insensitive comparer. Dim mySL2 As New SortedList(myHT, New CaseInsensitiveComparer()) Console.WriteLine("mySL2 (case-insensitive comparer):") Try mySL2.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL2) ' Create a SortedList using the specified CaseInsensitiveComparer , ' which is based on the Turkish culture (tr-TR), where "I" is not ' the uppercase version of "i". Dim myCul As New CultureInfo("tr-TR") Dim mySL3 As New SortedList(myHT, New CaseInsensitiveComparer(myCul)) Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):") Try mySL3.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL3) ' Create a SortedList using the ' StringComparer.InvariantCultureIgnoreCase value. Dim mySL4 As New SortedList(myHT, StringComparer.InvariantCultureIgnoreCase) Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):") Try mySL4.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL4) End Sub 'Main Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList) Console.WriteLine(" -KEY- -VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(" {0,-6}: {1}", _ myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesSortedList 'This code produces the following output. Results vary depending on the system's culture settings. ' 'mySL1 (default): ' -KEY- -VALUE- ' first : Ola! ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL2 (case-insensitive comparer): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL3 (case-insensitive comparer, Turkish culture): ' -KEY- -VALUE- ' FIRST : Hello ' first : Ola! ' SECOND: World ' THIRD : ! ' 'mySL4 (InvariantCultureIgnoreCase): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : !
using System; using System.Collections; using System.Globalization; public class SamplesSortedList { public static void Main() { // Create the dictionary. Hashtable myHT = new Hashtable(); myHT.Add("FIRST", "Hello"); myHT.Add("SECOND", "World"); myHT.Add("THIRD", "!"); // Create a SortedList using the default comparer. SortedList mySL1 = new SortedList(myHT); Console.WriteLine("mySL1 (default):"); try { mySL1.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL1); // Create a SortedList using the specified case-insensitive comparer. SortedList mySL2 = new SortedList(myHT, new CaseInsensitiveComparer()); Console.WriteLine("mySL2 (case-insensitive comparer):"); try { mySL2.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL2); // Create a SortedList using the specified CaseInsensitiveComparer , // which is based on the Turkish culture (tr-TR), where "I" is not // the uppercase version of "i". CultureInfo myCul = new CultureInfo("tr-TR"); SortedList mySL3 = new SortedList(myHT, new CaseInsensitiveComparer(myCul)); Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):"); try { mySL3.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL3); // Create a SortedList using the // StringComparer.InvariantCultureIgnoreCase value. SortedList mySL4 = new SortedList( myHT, StringComparer.InvariantCultureIgnoreCase); Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):"); try { mySL4.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL4); } public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine(" -KEY- -VALUE-"); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(" {0,-6}: {1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } } /* This code produces the following output. Results vary depending on the system's culture settings. mySL1 (default): -KEY- -VALUE- first : Ola! FIRST : Hello SECOND: World THIRD : ! mySL2 (case-insensitive comparer): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! mySL3 (case-insensitive comparer, Turkish culture): -KEY- -VALUE- FIRST : Hello first : Ola! SECOND: World THIRD : ! mySL4 (InvariantCultureIgnoreCase): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList コンストラクタ (IComparer)
アセンブリ: mscorlib (mscorlib.dll 内)


要素は、指定した IComparer 実装に基づいて並べ替えられます。comparer が null 参照 (Visual Basic では Nothing) の場合、各キーの IComparable 実装が使用されます。したがって、各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイスを実装する必要があります。
SortedList の容量は、SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

異なる SortedList コンストラクタを使用してコレクションを作成し、各コレクションの動作の違いを示すコード例を次に示します。
Imports System Imports System.Collections Imports System.Globalization Public Class SamplesSortedList Public Shared Sub Main() ' Create a SortedList using the default comparer. Dim mySL1 As New SortedList() Console.WriteLine("mySL1 (default):") mySL1.Add("FIRST", "Hello") mySL1.Add("SECOND", "World") mySL1.Add("THIRD", "!") Try mySL1.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL1) ' Create a SortedList using the specified case-insensitive comparer. Dim mySL2 As New SortedList(New CaseInsensitiveComparer()) Console.WriteLine("mySL2 (case-insensitive comparer):") mySL2.Add("FIRST", "Hello") mySL2.Add("SECOND", "World") mySL2.Add("THIRD", "!") Try mySL2.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL2) ' Create a SortedList using the specified CaseInsensitiveComparer , ' which is based on the Turkish culture (tr-TR), where "I" is not ' the uppercase version of "i". Dim myCul As New CultureInfo("tr-TR") Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul)) Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):") mySL3.Add("FIRST", "Hello") mySL3.Add("SECOND", "World") mySL3.Add("THIRD", "!") Try mySL3.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL3) ' Create a SortedList using the ' StringComparer.InvariantCultureIgnoreCase value. Dim mySL4 As New SortedList( _ StringComparer.InvariantCultureIgnoreCase) Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):") mySL4.Add("FIRST", "Hello") mySL4.Add("SECOND", "World") mySL4.Add("THIRD", "!") Try mySL4.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL4) End Sub 'Main Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList) Console.WriteLine(" -KEY- -VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(" {0,-6}: {1}", _ myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesSortedList 'This code produces the following output. Results vary depending on the system's culture settings. ' 'mySL1 (default): ' -KEY- -VALUE- ' first : Ola! ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL2 (case-insensitive comparer): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL3 (case-insensitive comparer, Turkish culture): ' -KEY- -VALUE- ' FIRST : Hello ' first : Ola! ' SECOND: World ' THIRD : ! ' 'mySL4 (InvariantCultureIgnoreCase): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : !
using System; using System.Collections; using System.Globalization; public class SamplesSortedList { public static void Main() { // Create a SortedList using the default comparer. SortedList mySL1 = new SortedList(); Console.WriteLine("mySL1 (default):"); mySL1.Add("FIRST", "Hello"); mySL1.Add("SECOND", "World"); mySL1.Add("THIRD", "!"); try { mySL1.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL1); // Create a SortedList using the specified case-insensitive comparer. SortedList mySL2 = new SortedList(new CaseInsensitiveComparer()); Console.WriteLine("mySL2 (case-insensitive comparer):"); mySL2.Add("FIRST", "Hello"); mySL2.Add("SECOND", "World"); mySL2.Add("THIRD", "!"); try { mySL2.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL2); // Create a SortedList using the specified CaseInsensitiveComparer , // which is based on the Turkish culture (tr-TR), where "I" is not // the uppercase version of "i". CultureInfo myCul = new CultureInfo("tr-TR"); SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul)); Console.WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):"); mySL3.Add("FIRST", "Hello"); mySL3.Add("SECOND", "World"); mySL3.Add("THIRD", "!"); try { mySL3.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL3); // Create a SortedList using the // StringComparer.InvariantCultureIgnoreCase value. SortedList mySL4 = new SortedList( StringComparer.InvariantCultureIgnoreCase); Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):"); mySL4.Add("FIRST", "Hello"); mySL4.Add("SECOND", "World"); mySL4.Add("THIRD", "!"); try { mySL4.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL4); } public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine(" -KEY- -VALUE-"); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(" {0,-6}: {1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } } /* This code produces the following output. Results vary depending on the system's culture settings. mySL1 (default): -KEY- -VALUE- first : Ola! FIRST : Hello SECOND: World THIRD : ! mySL2 (case-insensitive comparer): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! mySL3 (case-insensitive comparer, Turkish culture): -KEY- -VALUE- FIRST : Hello first : Ola! SECOND: World THIRD : ! mySL4 (InvariantCultureIgnoreCase): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList コンストラクタ

名前 | 説明 |
---|---|
SortedList () | 空で、既定の初期量を備え、既定の IComparer を使用する、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (ジェネリック IComparer) | 空で、既定の初期量を備え、指定した IComparer を使用する、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (ジェネリック IDictionary) | 指定した IDictionary からコピーした要素を格納し、コピーした要素の数を格納できるだけの容量を備え、既定の IComparer を使用する、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (Int32) | 空で、指定した初期量を備え、既定の IComparer を使用する、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (ジェネリック IDictionary, ジェネリック IComparer) | 指定した IDictionary からコピーした要素を格納し、コピーした要素の数を格納できるだけの容量を備え、指定した IComparer を使用する、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
SortedList (Int32, ジェネリック IComparer) | 空で、指定した初期量を備え、指定した IComparer を使用する、SortedList クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |

関連項目
SortedList ジェネリック クラスSortedList メンバ
System.Collections.Generic 名前空間
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedList コンストラクタ (ジェネリック IComparer)
アセンブリ: System (system.dll 内)


SortedList 内のすべてのキーは指定した比較演算子に従って一意である必要があります。
このコンストラクタは、SortedList の初期量として既定値を使用します。初期量を設定するには、SortedList(Int32,ジェネリック IComparer) コンストラクタを使用します。コレクションの最終的なサイズを推定できる場合、初期量を指定すると、SortedList に要素を追加するときにサイズ変更操作を何度も実行する必要がなくなります。

現在のカルチャの大文字小文字を区別しない比較演算子を使って並べ替えられたリストを作成するコード例を次に示します。この例では、小文字のキーを含む要素と大文字のキーを含む要素を合わせて 4 つの要素を追加します。次に、既存のキーとの違いが大文字と小文字の違いだけであるキーを含む要素の追加を試行し、結果の例外をキャッチし、エラー メッセージを表示します。最後に、大文字小文字を区別しない並べ替え順で要素を表示します。
Imports System Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new sorted list of strings, with string keys and ' a case-insensitive comparer for the current culture. Dim openWith As New SortedList(Of String, String)( _ StringComparer.CurrentCultureIgnoreCase) ' Add some elements to the list. 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 sorted list.") End Try ' List the contents of the sorted list. 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 sorted list. ' '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 sorted list of strings, with string keys and // a case-insensitive comparer for the current culture. SortedList<string, string> openWith = new SortedList<string, string>( StringComparer.CurrentCultureIgnoreCase); // Add some elements to the list. 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 sorted list."); } // List the contents of the sorted list. 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 sorted list. Key = bmp, Value = paint.exe Key = DIB, Value = paint.exe Key = rtf, Value = wordpad.exe Key = txt, Value = notepad.exe */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
IComparer ジェネリック インターフェイス
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedList コンストラクタ (ジェネリック IDictionary)
アセンブリ: System (system.dll 内)

Public Sub New ( _ dictionary As IDictionary(Of TKey, TValue) _ )
Dim dictionary As IDictionary(Of TKey, TValue) Dim instance As New SortedList(Of TKey, TValue)(dictionary)
public SortedList ( IDictionary<TKey,TValue> dictionary )
public: SortedList ( IDictionary<TKey, TValue>^ dictionary )
public SortedList ( IDictionary<TKey,TValue> dictionary )
public function SortedList ( dictionary : IDictionary<TKey,TValue> )

例外の種類 | 条件 |
---|---|
ArgumentNullException | dictionary が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException |

SortedList 内のすべてのキーは、既定の比較演算子に従って一意である必要があります。同様に、元の dictionary 内のすべてのキーも、既定の比較演算子に従って一意である必要があります。
新しい SortedList の容量は、dictionary 内の要素数に設定されるので、リストの作成時にサイズの変更は行われません。
このコンストラクタは、TKey の既定の比較演算子を使用します。比較演算子を指定するには、SortedList(ジェネリック IDictionary,ジェネリック IComparer) コンストラクタを使用します。既定の比較演算子 Comparer.Default は、キーの型 TKey が System.IComparable を実装し、利用可能な場合はその実装を使用するかどうかをチェックします。それ以外の場合は、Comparer.Default によって、キーの型 TKey が System.IComparable を実装するかどうかをチェックします。キーの型 TKey がいずれのインターフェイスも実装しない場合は、comparer パラメータを受け取るコンストラクタ オーバーロードの System.Collections.Generic.IComparer 実装を指定できます。
dictionary のデータが並べ替え済みの場合、このコンストラクタは O(n) 操作です (n は dictionary 内の要素数)。そうでない場合は O(n*n) 操作です。

Dictionary を SortedList(ジェネリック IDictionary) コンストラクタに渡すことにより、SortedList を使用して、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 SortedList of strings with string keys, ' and initialize it with the contents of the Dictionary. Dim copy As New SortedList(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 SortedList of strings with string keys, // and initialize it with the contents of the Dictionary. SortedList<string, string> copy = new SortedList<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 */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
IDictionary ジェネリック インターフェイス
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedList コンストラクタ (Int32)
アセンブリ: System (system.dll 内)



SortedList 内のすべてのキーは既定の比較演算子に従って一意である必要があります。
SortedList の容量は、サイズ変更前に SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。
容量を減らすには、TrimExcess を呼び出すか、Capacity プロパティを明示的に設定します。容量を減らすと、メモリの再割り当てが行われ、SortedList 内のすべての要素がコピーされます。
このコンストラクタは、TKey の既定の比較演算子を使用します。比較演算子を指定するには、SortedList(Int32,ジェネリック IComparer) コンストラクタを使用します。既定の比較演算子 Comparer.Default は、キーの型 TKey が System.IComparable を実装し、利用可能な場合はその実装を使用するかどうかをチェックします。それ以外の場合は、Comparer.Default によって、キーの型 TKey が System.IComparable を実装するかどうかをチェックします。キーの型 TKey がいずれのインターフェイスも実装しない場合は、comparer パラメータを受け取るコンストラクタ オーバーロードの System.Collections.Generic.IComparer 実装を指定できます。

並べ替えられたリストを初期量 4 で作成し、4 つのエントリを設定するコード例を次に示します。
Imports System Imports System.Collections.Generic Public Class Example Public Shared Sub Main() ' Create a new sorted list of strings, with string keys and ' an initial capacity of 4. Dim openWith As New SortedList(Of String, String)(4) ' Add 4 elements to the list. 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 sorted list. 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: ' '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 sorted list of strings, with string keys and // an initial capacity of 4. SortedList<string, string> openWith = new SortedList<string, string>(4); // Add 4 elements to the list. 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 sorted list. 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: Key = bmp, Value = paint.exe Key = dib, Value = paint.exe Key = rtf, Value = wordpad.exe Key = txt, Value = notepad.exe */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
Capacity
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedList コンストラクタ (ジェネリック IDictionary, ジェネリック IComparer)
アセンブリ: 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 SortedList(Of TKey, TValue)(dictionary, comparer)
public SortedList ( IDictionary<TKey,TValue> dictionary, IComparer<TKey> comparer )
public: SortedList ( IDictionary<TKey, TValue>^ dictionary, IComparer<TKey>^ comparer )
public SortedList ( IDictionary<TKey,TValue> dictionary, IComparer<TKey> comparer )
public function SortedList ( dictionary : IDictionary<TKey,TValue>, comparer : IComparer<TKey> )

例外の種類 | 条件 |
---|---|
ArgumentNullException | dictionary が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException |

SortedList 内のすべてのキーは、指定した比較演算子に従って一意である必要があります。同様に、元の dictionary 内のすべてのキーも、指定した比較演算子に従って一意である必要があります。
新しい SortedList の容量は、dictionary 内の要素数に設定されるので、リストの作成時にサイズの変更は行われません。
dictionary のデータが並べ替え済みの場合、このコンストラクタは O(n) 操作です (n は dictionary 内の要素数)。そうでない場合は O(n*n) 操作です。

Dictionary を SortedList(ジェネリック IDictionary,ジェネリック IComparer) コンストラクタに渡すことにより、SortedList を使用して、大文字小文字を区別しない 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") ' Create a SortedList 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 SortedList(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 = 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"); // Create a SortedList of strings with string keys and a // case-insensitive equality comparer for the current culture , // and initialize it with the contents of the Dictionary. SortedList<string, string> copy = new SortedList<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 = Bmp, Value = paint.exe Key = DIB, Value = paint.exe Key = rtf, Value = wordpad.exe Key = txt, Value = notepad.exe */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
IDictionary ジェネリック インターフェイス
IComparer ジェネリック インターフェイス
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
SortedList コンストラクタ ()
アセンブリ: mscorlib (mscorlib.dll 内)


各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイスを実装する必要があります。要素は、SortedList に追加された各キーの IComparable 実装に基づいて並べ替えられます。
SortedList の容量は、SortedList が保持できる要素数になります。SortedList に要素を追加すると、必要に応じて、内部の配列の再割り当てによって容量が自動的に増加します。
コレクションのサイズを推定できる場合は、初期量を指定すると、SortedList に要素を追加するときに、サイズ変更操作を何度も実行する必要がなくなります。

異なる SortedList コンストラクタを使用してコレクションを作成し、各コレクションの動作の違いを示すコード例を次に示します。
Imports System Imports System.Collections Imports System.Globalization Public Class SamplesSortedList Public Shared Sub Main() ' Create a SortedList using the default comparer. Dim mySL1 As New SortedList() Console.WriteLine("mySL1 (default):") mySL1.Add("FIRST", "Hello") mySL1.Add("SECOND", "World") mySL1.Add("THIRD", "!") Try mySL1.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL1) ' Create a SortedList using the specified case-insensitive comparer. Dim mySL2 As New SortedList(New CaseInsensitiveComparer()) Console.WriteLine("mySL2 (case-insensitive comparer):") mySL2.Add("FIRST", "Hello") mySL2.Add("SECOND", "World") mySL2.Add("THIRD", "!") Try mySL2.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL2) ' Create a SortedList using the specified CaseInsensitiveComparer , ' which is based on the Turkish culture (tr-TR), where "I" is not ' the uppercase version of "i". Dim myCul As New CultureInfo("tr-TR") Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul)) Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):") mySL3.Add("FIRST", "Hello") mySL3.Add("SECOND", "World") mySL3.Add("THIRD", "!") Try mySL3.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL3) ' Create a SortedList using the ' StringComparer.InvariantCultureIgnoreCase value. Dim mySL4 As New SortedList( _ StringComparer.InvariantCultureIgnoreCase) Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):") mySL4.Add("FIRST", "Hello") mySL4.Add("SECOND", "World") mySL4.Add("THIRD", "!") Try mySL4.Add("first", "Ola!") Catch e As ArgumentException Console.WriteLine(e) End Try PrintKeysAndValues(mySL4) End Sub 'Main Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList) Console.WriteLine(" -KEY- -VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(" {0,-6}: {1}", _ myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub 'PrintKeysAndValues End Class 'SamplesSortedList 'This code produces the following output. Results vary depending on the system's culture settings. ' 'mySL1 (default): ' -KEY- -VALUE- ' first : Ola! ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL2 (case-insensitive comparer): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : ! ' 'mySL3 (case-insensitive comparer, Turkish culture): ' -KEY- -VALUE- ' FIRST : Hello ' first : Ola! ' SECOND: World ' THIRD : ! ' 'mySL4 (InvariantCultureIgnoreCase): 'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value) ' at SamplesSortedList.Main() ' -KEY- -VALUE- ' FIRST : Hello ' SECOND: World ' THIRD : !
using System; using System.Collections; using System.Globalization; public class SamplesSortedList { public static void Main() { // Create a SortedList using the default comparer. SortedList mySL1 = new SortedList(); Console.WriteLine("mySL1 (default):"); mySL1.Add("FIRST", "Hello"); mySL1.Add("SECOND", "World"); mySL1.Add("THIRD", "!"); try { mySL1.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL1); // Create a SortedList using the specified case-insensitive comparer. SortedList mySL2 = new SortedList(new CaseInsensitiveComparer()); Console.WriteLine("mySL2 (case-insensitive comparer):"); mySL2.Add("FIRST", "Hello"); mySL2.Add("SECOND", "World"); mySL2.Add("THIRD", "!"); try { mySL2.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL2); // Create a SortedList using the specified CaseInsensitiveComparer , // which is based on the Turkish culture (tr-TR), where "I" is not // the uppercase version of "i". CultureInfo myCul = new CultureInfo("tr-TR"); SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul)); Console.WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):"); mySL3.Add("FIRST", "Hello"); mySL3.Add("SECOND", "World"); mySL3.Add("THIRD", "!"); try { mySL3.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL3); // Create a SortedList using the // StringComparer.InvariantCultureIgnoreCase value. SortedList mySL4 = new SortedList( StringComparer.InvariantCultureIgnoreCase); Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):"); mySL4.Add("FIRST", "Hello"); mySL4.Add("SECOND", "World"); mySL4.Add("THIRD", "!"); try { mySL4.Add("first", "Ola!"); } catch (ArgumentException e) { Console.WriteLine(e); } PrintKeysAndValues(mySL4); } public static void PrintKeysAndValues(SortedList myList) { Console.WriteLine(" -KEY- -VALUE-"); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(" {0,-6}: {1}", myList.GetKey(i), myList.GetByIndex(i)); } Console.WriteLine(); } } /* This code produces the following output. Results vary depending on the system's culture settings. mySL1 (default): -KEY- -VALUE- first : Ola! FIRST : Hello SECOND: World THIRD : ! mySL2 (case-insensitive comparer): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! mySL3 (case-insensitive comparer, Turkish culture): -KEY- -VALUE- FIRST : Hello first : Ola! SECOND: World THIRD : ! mySL4 (InvariantCultureIgnoreCase): System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first' at System.Collections.SortedList.Add(Object key, Object value) at SamplesSortedList.Main() -KEY- -VALUE- FIRST : Hello SECOND: World THIRD : ! */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList コンストラクタ ()
アセンブリ: System (system.dll 内)


SortedList 内のすべてのキーは既定の比較演算子に従って一意である必要があります。
このコンストラクタは、SortedList の初期量として既定値を使用します。初期量を設定するには、SortedList(Int32) コンストラクタを使用します。コレクションの最終的なサイズを推定できる場合、初期量を指定すると、SortedList に要素を追加するときにサイズ変更操作を何度も実行する必要がなくなります。
このコンストラクタは、TKey の既定の比較演算子を使用します。比較演算子を指定するには、SortedList(ジェネリック IComparer) コンストラクタを使用します。既定の比較演算子 Comparer.Default は、キーの型 TKey が System.IComparable を実装し、利用可能な場合はその実装を使用するかどうかをチェックします。それ以外の場合は、Comparer.Default によって、キーの型 TKey が System.IComparable を実装するかどうかをチェックします。キーの型 TKey がいずれのインターフェイスも実装しない場合は、comparer パラメータを受け取るコンストラクタ オーバーロードの System.Collections.Generic.IComparer 実装を指定できます。

文字列キーを含む文字列の空の SortedList を作成し、Add メソッドを使用していくつかの要素を追加するコード例を次に示します。この例では、重複するキーを追加しようとすると、Add メソッドが ArgumentException をスローすることを示します。
このコード例は、SortedList クラスのトピックで取り上げているコード例の一部分です。
' Create a new sorted list of strings, with string ' keys. Dim openWith As New SortedList(Of String, String) ' Add some elements to the list. 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 list. Try openWith.Add("txt", "winword.exe") Catch Console.WriteLine("An element with Key = ""txt"" already exists.") End Try
// Create a new sorted list of strings, with string // keys. SortedList<string, string> openWith = new SortedList<string, string>(); // Add some elements to the list. 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 list. try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
Comparer.Default プロパティ
IComparable ジェネリック インターフェイス
IComparable インターフェイス
Weblioに収録されているすべての辞書からSortedList コンストラクタを検索する場合は、下記のリンクをクリックしてください。

- SortedList コンストラクタのページへのリンク