CircularSortedListTKey, TValueAdd Method

Adds an element with the provided key and value to the CircularSortedListTKey, TValue.

Definition

Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
C#
public int Add(
	TKey key,
	TValue value
)

Parameters

key  TKey
The key of the element to add.
value  TValue
The value of the element to add. The value can be for reference and NullableT types.

Return Value

Int32
The zero-based index in the CircularSortedListTKey, TValue at which the key-value pair has been added.

Remarks

A key cannot be , but a value can be, if the type of values in the sorted list, TValue, is a reference or NullableT type.

You can also use the indexer to add new elements by setting the value of a key that does not exist in the CircularSortedListTKey, TValue. for example:

C#
myCollection["myNonexistentKey"] = myValue;
However, if the specified key already exists in the CircularSortedListTKey, TValue, setting the indexer overwrites the old value. In contrast, the Add method does not modify existing elements.

If Count already equals Capacity, the capacity of the CircularSortedListTKey, TValue is increased by automatically reallocating the array in internal CircularListT, and the existing elements are copied to the new array before the new element is added.

This method is an O(n) operation for unsorted data, where n is Count. It is an O(1) operation if the new element is added at the end or the head of the list. If insertion causes a resize, the operation is O(n).

Exceptions

ArgumentNullExceptionkey is .
ArgumentExceptionAn element with the same key already exists in the CircularSortedListTKey, TValue.

See Also