SortedList<TKey,TValue>.Add(TKey, TValue) メソッド

定義

指定したキーおよび値を持つ要素を SortedList<TKey,TValue> に追加します。

public:
 virtual void Add(TKey key, TValue value);
public void Add (TKey key, TValue value);
abstract member Add : 'Key * 'Value -> unit
override this.Add : 'Key * 'Value -> unit
Public Sub Add (key As TKey, value As TValue)

パラメーター

key
TKey

追加する要素のキー。

value
TValue

追加する要素の値。 参照型の場合は null の値を使用できます。

実装

例外

keynullです。

同じキーを持つ要素が、SortedList<TKey,TValue> に既に存在します。

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

このコード例は、SortedList<TKey,TValue> クラスのために提供されている大規模な例の一部です。

// Create a new sorted list of strings, with string
// keys.
SortedList<String^, String^>^ openWith =
    gcnew 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.");
}
// 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.");
}
' 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.
let openWith = 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");
with
    | :? ArgumentException ->
        printfn "An element with Key = \"txt\" already exists."

注釈

キーを に nullすることはできませんが、並べ替えられたリストの値の型が参照型である場合は、 TValue値を 指定できます。

また、 プロパティを Item[] 使用して、 に存在しないキーの値を設定することで、新しい要素を SortedList<TKey,TValue>追加することもできます。たとえば、 myCollection["myNonexistentKey"] = myValueなどです。 ただし、 に指定したキーが既に SortedList<TKey,TValue>存在する場合は、 プロパティを Item[] 設定すると、古い値が上書きされます。 これに対し、 メソッドは既存の Add 要素を変更しません。

が既に と等しいCapacity場合Count、 のSortedList<TKey,TValue>容量は内部配列を自動的に再割り当てすることで増加し、新しい要素が追加される前に既存の要素が新しい配列にコピーされます。

このメソッドは、並べ替えされていないデータに対する O(n) 操作です。ここで n 、 は Countです。 リストの末尾に新しい要素が追加された場合は、O(log n) 操作です。 挿入によってサイズが変更される場合、操作は O(n) になります。

適用対象

こちらもご覧ください