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이 될 수 있습니다.

구현

예외

key이(가) null인 경우

같은 키를 가진 요소가 이미 SortedList<TKey,TValue>에 있는 경우

예제

다음 코드 예제에서는 문자열 키가 있는 빈 SortedList<TKey,TValue> 문자열을 만들고 메서드를 Add 사용하여 일부 요소를 추가합니다. 이 예제에서는 메서드가 Add 중복 키를 추가하려고 할 때 throw 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

설명

키는 사용할 null수 없지만 정렬된 목록 TValue의 값 형식이 참조 형식인 경우 값이 될 수 있습니다.

에 없는 SortedList<TKey,TValue>키의 값을 설정 하 여 새 요소를 추가 하는 속성을 사용할 Item[] 수도 있습니다. 예를 들어. myCollection["myNonexistentKey"] = myValue 그러나 지정된 키가 이미 있는 SortedList<TKey,TValue>경우 속성을 설정 Item[] 하면 이전 값이 덮어씁니다. 반면, 메서드는 Add 기존 요소를 수정하지 않습니다.

이미 같은 Capacity경우 Count 내부 배열을 자동으로 다시 할당하여 용량 SortedList<TKey,TValue> 이 증가하고 새 요소가 추가되기 전에 기존 요소가 새 배열에 복사됩니다.

이 메서드는 정렬되지 않은 데이터에 n 대한 O(n) 작업입니다Count. 목록의 끝에 새 요소가 추가되면 O(로그 n) 작업입니다. 삽입으로 인해 크기가 조정되는 경우 작업은 O(n)입니다.

적용 대상

추가 정보