SortedList コンストラクタとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SortedList コンストラクタの意味・解説 

SortedList コンストラクタ (IDictionary)

指定したディクショナリからコピーした要素格納しコピーした要素の数と同じ初期量を備え、各キーによって実装されている IComparable インターフェイスに従って並べ替えられた、SortedList クラス新しインスタンス初期化します。

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

Public Sub New ( _
    d As IDictionary _
)
Dim d As IDictionary

Dim instance As New SortedList(d)
public SortedList (
    IDictionary d
)
public:
SortedList (
    IDictionary^ d
)
public SortedList (
    IDictionary d
)
public function SortedList (
    d : IDictionary
)

パラメータ

d

新しい SortedList にコピーする IDictionary。

例外例外
例外種類条件

ArgumentNullException

dnull 参照 (Visual Basic では Nothing) です。

InvalidCastException

d1 つ上の要素が、IComparable インターフェイス実装していません。

解説解説

キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイス実装する必要があります要素は、SortedList追加された各キーIComparable 実装基づいて並べ替えられます。

Hashtable は、このコンストラクタに渡すことができる IDictionary 実装一例です。新しSortedList には、Hashtable並べ替えられたキーと値のコピー格納されます。

SortedList容量は、SortedList保持できる要素数になりますSortedList要素追加すると、必要に応じて内部配列の再割り当てによって容量自動的に増加します。

コレクションサイズ推定できる場合は、初期量を指定すると、SortedList要素追加するときに、サイズ変更操作何度も実行する必要がなくなります

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

使用例使用例

異な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 : !

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

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

空で、指定した初期量を備え指定した IComparer使用するSortedList クラス新しインスタンス初期化します。

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

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

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

パラメータ

capacity

SortedList が格納できる要素数の初期値

comparer

キー比較する場合使用する IComparer 実装

または

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

例外例外
例外種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

解説解説
使用例使用例

初期量 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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ

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 クラス
SortedList メンバ
System.Collections 名前空間
IComparable インターフェイス
Capacity

その他の技術情報

カルチャを認識しないコレクション操作実行

SortedList コンストラクタ (Int32)

空で、既定初期量を備えSortedList追加された各キーによって実装されている IComparable インターフェイスに従って並べ替えられた、SortedList クラス新しインスタンス初期化します。

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

Public Sub New ( _
    initialCapacity As Integer _
)
Dim initialCapacity As Integer

Dim instance As New SortedList(initialCapacity)
public SortedList (
    int initialCapacity
)
public:
SortedList (
    int initialCapacity
)
public SortedList (
    int initialCapacity
)
public function SortedList (
    initialCapacity : int
)

パラメータ

initialCapacity

SortedList が格納できる要素数の初期値

例外例外
例外種類条件

ArgumentOutOfRangeException

initialCapacity が 0 未満です。

OutOfMemoryException

メモリ不足しているため、指定されinitialCapacity を持つ SortedList作成できません。

解説解説

キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイス実装する必要があります要素は、SortedList追加された各キーIComparable 実装基づいて並べ替えられます。

SortedList容量は、SortedList保持できる要素数になりますSortedList要素追加すると、必要に応じて内部配列の再割り当てによって容量自動的に増加します。

コレクションサイズ推定できる場合は、初期量を指定すると、SortedList要素追加するときに、サイズ変更操作何度も実行する必要がなくなります

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

使用例使用例

異な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 : !

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ (IComparer, Int32)

空で、指定した初期量を備え指定した IComparer インターフェイスに従って並べ替えられた、SortedList クラス新しインスタンス初期化します。

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

Public Sub New ( _
    comparer As IComparer, _
    capacity As Integer _
)
Dim comparer As IComparer
Dim capacity As Integer

Dim instance As New SortedList(comparer,
 capacity)
public SortedList (
    IComparer comparer,
    int capacity
)
public:
SortedList (
    IComparer^ comparer, 
    int capacity
)
public SortedList (
    IComparer comparer, 
    int capacity
)
public function SortedList (
    comparer : IComparer, 
    capacity : int
)

パラメータ

comparer

キー比較する場合使用する IComparer 実装

または

キーの IComparable 実装使用する場合null 参照 (Visual Basic では Nothing)。

capacity

SortedList が格納できる要素数の初期値

例外例外
例外種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

OutOfMemoryException

メモリ不足しているため、指定されcapacity を持つ SortedList作成できません。

解説解説

要素は、指定した IComparer 実装基づいて並べ替えられます。comparernull 参照 (Visual Basic では Nothing) の場合、各キーIComparable 実装使用されます。たがって、各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイス実装する必要があります

SortedList容量は、SortedList保持できる要素数になりますSortedList要素追加すると、必要に応じて内部配列の再割り当てによって容量自動的に増加します。

コレクションサイズ推定できる場合は、初期量を指定すると、SortedList要素追加するときに、サイズ変更操作何度も実行する必要がなくなります

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

使用例使用例

異な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 : !

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ (IDictionary, IComparer)

指定したディクショナリからコピーした要素格納しコピーした要素の数と同じ初期量を備え指定した IComparer インターフェイスに従って並べ替えられた、SortedList クラス新しインスタンス初期化します。

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

Public Sub New ( _
    d As IDictionary, _
    comparer As IComparer _
)
Dim d As IDictionary
Dim comparer As IComparer

Dim instance As New SortedList(d,
 comparer)
public SortedList (
    IDictionary d,
    IComparer comparer
)
public:
SortedList (
    IDictionary^ d, 
    IComparer^ comparer
)
public SortedList (
    IDictionary d, 
    IComparer comparer
)
public function SortedList (
    d : IDictionary, 
    comparer : IComparer
)

パラメータ

d

新しい SortedList にコピーする IDictionary。

comparer

キー比較する場合使用する IComparer 実装

または

キーの IComparable 実装使用する場合null 参照 (Visual Basic では Nothing)。

例外例外
例外種類条件

ArgumentNullException

dnull 参照 (Visual Basic では Nothing) です。

InvalidCastException

comparernull 参照 (Visual Basic では Nothing) で、d1 つ上の要素IComparable インターフェイス実装していません。

解説解説

要素は、指定した IComparer 実装基づいて並べ替えられます。comparernull 参照 (Visual Basic では Nothing) の場合、各キーIComparable 実装使用されます。たがって、各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイス実装する必要があります

Hashtable は、このコンストラクタに渡すことができる IDictionary 実装一例です。新しSortedList には、Hashtable並べ替えられたキーと値のコピー格納されます。

SortedList容量は、SortedList保持できる要素数になりますSortedList要素追加すると、必要に応じて内部配列の再割り当てによって容量自動的に増加します。

コレクションサイズ推定できる場合は、初期量を指定すると、SortedList要素追加するときに、サイズ変更操作何度も実行する必要がなくなります

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

使用例使用例

異な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 : !

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ (IComparer)

空で、既定初期量を備え指定した IComparer インターフェイスに従って並べ替えられた、SortedList クラス新しインスタンス初期化します。

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

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

Dim instance As New SortedList(comparer)
public SortedList (
    IComparer comparer
)
public:
SortedList (
    IComparer^ comparer
)
public SortedList (
    IComparer comparer
)
public function SortedList (
    comparer : IComparer
)

パラメータ

comparer

キー比較する場合使用する IComparer 実装

または

キーの IComparable 実装使用する場合null 参照 (Visual Basic では Nothing)。

解説解説

要素は、指定した IComparer 実装基づいて並べ替えられます。comparernull 参照 (Visual Basic では Nothing) の場合、各キーIComparable 実装使用されます。たがって、各キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイス実装する必要があります

SortedList容量は、SortedList保持できる要素数になりますSortedList要素追加すると、必要に応じて内部配列の再割り当てによって容量自動的に増加します。

コレクションサイズ推定できる場合は、初期量を指定すると、SortedList要素追加するときに、サイズ変更操作何度も実行する必要がなくなります

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

使用例使用例

異な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 : !

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ

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)

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

空で、既定初期量を備え指定した IComparer使用する、SortedList クラス新しインスタンス初期化します。

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

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

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

パラメータ

comparer

キー比較する場合使用する IComparer 実装

または

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

解説解説
使用例使用例

現在のカルチャの大文字小文字区別しない比較演算子使って並べ替えられたリスト作成するコード例次に示します。この例では、小文字キーを含む要素大文字キーを含む要素合わせて 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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

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

指定した IDictionary からコピーした要素格納しコピーした要素の数を格納できるだけの容量備え既定の IComparer を使用するSortedList クラス新しインスタンス初期化します。

名前空間: 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 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>
)

パラメータ

dictionary

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

例外例外
例外種類条件

ArgumentNullException

dictionarynull 参照 (Visual Basic では Nothing) です。

ArgumentException

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

解説解説

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) 操作です (ndictionary 内の要素数)。そうでない場合は O(n*n) 操作です。

使用例使用例

DictionarySortedList(ジェネリック 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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ (Int32)

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

空で、指定した初期量を備え既定の IComparer を使用するSortedList クラス新しインスタンス初期化します。

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

例外例外
例外種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

解説解説

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 実装指定できます

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

使用例使用例

並べ替えられたリスト初期量 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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

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

指定した IDictionary からコピーした要素格納しコピーした要素の数を格納できるだけの容量備え指定した IComparer使用するSortedList クラス新しインスタンス初期化します。

名前空間: 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 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>
)

パラメータ

dictionary

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

comparer

キー比較する場合使用する IComparer 実装

または

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

例外例外
例外種類条件

ArgumentNullException

dictionarynull 参照 (Visual Basic では Nothing) です。

ArgumentException

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

解説解説
使用例使用例

DictionarySortedList(ジェネリック 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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ ()

空で、既定初期量を備え、SortedList に追加された各キーによって実装されている IComparable インターフェイスに従って並べ替えられた、SortedList クラス新しインスタンス初期化します。

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

public SortedList ()
public:
SortedList ()
public SortedList ()
解説解説

キーは、SortedList 内の他のキーとの比較を行うことができるように、IComparable インターフェイス実装する必要があります要素は、SortedList追加された各キーIComparable 実装基づいて並べ替えられます。

SortedList容量は、SortedList保持できる要素数になりますSortedList要素追加すると、必要に応じて内部配列の再割り当てによって容量自動的に増加します。

コレクションサイズ推定できる場合は、初期量を指定すると、SortedList要素追加するときに、サイズ変更操作何度も実行する必要がなくなります

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

使用例使用例

異な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 : !

*/

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

SortedList コンストラクタ ()

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

空で、既定初期量を備え既定の IComparer を使用する、SortedList クラス新しインスタンス初期化します。

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

Dim instance As New SortedList(Of
 TKey, TValue)
public SortedList ()
public:
SortedList ()
public SortedList ()
解説解説

SortedList 内のすべてのキー既定比較演算子に従って一意である必要があります

このコンストラクタは、SortedList初期量として既定値使用します初期量を設定するには、SortedList(Int32) コンストラクタ使用しますコレクション最終的なサイズ推定できる場合初期量を指定すると、SortedList要素追加するときにサイズ変更操作何度も実行する必要がなくなります

このコンストラクタは、TKey既定比較演算子使用します比較演算子指定するには、SortedList(ジェネリック IComparer) コンストラクタ使用します既定比較演算子 Comparer.Default は、キーの型 TKey が System.IComparable を実装し、利用可能場合その実装を使用するかどうかチェックしますそれ以外場合は、Comparer.Default によって、キーの型 TKey が System.IComparable を実装するかどうかチェックしますキーの型 TKeyいずれのインターフェイス実装ない場合は、comparer パラメータ受け取コンストラクタ オーバーロードSystem.Collections.Generic.IComparer 実装指定できます

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

使用例使用例

文字列キーを含む文字列の空の 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.");
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からSortedList コンストラクタを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSortedList コンストラクタを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSortedList コンストラクタ を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「SortedList コンストラクタ」の関連用語

SortedList コンストラクタのお隣キーワード
検索ランキング

   

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



SortedList コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS