MyTetra Share
Делитесь знаниями!
Метод Dictionary(Of TKey, TValue).Add (TKey, TValue)
Время создания: 24.03.2018 23:56
Раздел: VB
Запись: xintrea/mytetra_db_adgaver_new/master/base/15219249957wgk34objx/text.html на raw.githubusercontent.com

Метод Dictionary(OfTKey,TValue).Add (TKey,TValue)

.NET Framework (current version)


 

Опубликовано: Октябрь 2016

Добавляет указанные ключ и значение в словарь.

Пространство имен:   System.Collections.Generic
Сборка:  mscorlib (в mscorlib.dll)

Синтаксис

C#

C++

F#

VB

Public Sub Add (

key As TKey,

value As TValue

)


Параметры

key

Type: TKey

Ключ добавляемого элемента.

value

Type: TValue

Добавляемое значение элемента. Для ссылочных типов допускается значение null.

Реализует

IDictionary

(Of TKey, TValue).Add(TKey, TValue)

Исключения


Exception

Condition

ArgumentNullException

Свойство key имеет значение null.

ArgumentException

Элемент с таким ключом уже существует в Dictionary(Of TKey, TValue) .

Комментарии

You can also use the P:System.Collections.Generic.Dictionary`2.Item(`0) property to add new elements by setting the value of a key that does not exist in the T:System.Collections.Generic.Dictionary`2; for example, myCollection[myKey] = myValue (in Visual Basic, myCollection(myKey) = myValue). However, if the specified key already exists in the T:System.Collections.Generic.Dictionary`2, setting the P:System.Collections.Generic.Dictionary`2.Item(`0) property overwrites the old value. In contrast, the M:System.Collections.Generic.Dictionary`2.Add(`0,`1)method throws an exception if a value with the specified key already exists.

If the P:System.Collections.Generic.Dictionary`2.Count property value already equals the capacity, the capacity of the T:System.Collections.Generic.Dictionary`2 is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

A key cannot be null, but a value can be, if TValue is a reference type.

If P:System.Collections.Generic.Dictionary`2.Count is less than the capacity, this method approaches an O(1) operation. If the capacity must be increased to accommodate the new element, this method becomes an O(n) operation, where n is P:System.Collections.Generic.Dictionary`2.Count.

Примеры

The following code example creates an empty T:System.Collections.Generic.Dictionary`2 of strings with string keys and uses the M:System.Collections.Generic.Dictionary`2.Add(`0,`1) method to add some elements. The example demonstrates that the M:System.Collections.Generic.Dictionary`2.Add(`0,`1) method throws an T:System.ArgumentException when attempting to add a duplicate key.

This code example is part of a larger example provided for the T:System.Collections.Generic.Dictionary`2 class.

C#

C++

VB

' Create a new dictionary of strings, with string keys.

'

Dim openWith As New Dictionary(Of String, String)


' Add some elements to the dictionary. There are no

' duplicate keys, but some of the values are duplicates.

openWith.Add("txt", "notepad.exe")

openWith.Add("bmp", "paint.exe")

openWith.Add("dib", "paint.exe")

openWith.Add("rtf", "wordpad.exe")


' The Add method throws an exception if the new key is

' already in the dictionary.

Try

openWith.Add("txt", "winword.exe")

Catch

Console.WriteLine("An element with Key = ""txt"" already exists.")

End Try


Информация о версии

Универсальная платформа Windows
Доступно с 8
.NET Framework
Доступно с 2.0
Переносимая библиотека классов
Поддерживается в:
переносимые платформы .NET
Silverlight
Доступно с 2.0
Windows Phone Silverlight
Доступно с 7.0
Windows Phone
Доступно с 8.1

Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования