Skip to content

M_CodeJam_Collections_DictionaryExtensions_AddOrUpdate__2

andrewvk edited this page Mar 30, 2016 · 10 revisions

DictionaryExtensions.AddOrUpdate(TKey, TValue) Method (IDictionary(TKey, TValue), TKey, Func(TKey, TValue), Func(TKey, TValue, TValue))

[This is preliminary documentation and is subject to change.]

Adds a key/value pair to the IDictionary(TKey, TValue) if the key does not already exist, or updates a key/value pair IDictionary(TKey, TValue) by using the specified function if the key already exists.

Namespace: CodeJam.Collections
Assembly: CodeJam (in CodeJam.dll) Version: 1.0.0.0 (1.0.0.0)

Syntax

C#

public static TValue AddOrUpdate<TKey, TValue>(
	[NotNullAttribute] this IDictionary<TKey, TValue> dictionary,
	TKey key,
	[NotNullAttribute][InstantHandleAttribute] Func<TKey, TValue> addValueFactory,
	[NotNullAttribute][InstantHandleAttribute] Func<TKey, TValue, TValue> updateValueFactory
)

VB

<ExtensionAttribute>
Public Shared Function AddOrUpdate(Of TKey, TValue) ( 
	<NotNullAttribute> dictionary As IDictionary(Of TKey, TValue),
	key As TKey,
	<NotNullAttribute><InstantHandleAttribute> addValueFactory As Func(Of TKey, TValue),
	<NotNullAttribute><InstantHandleAttribute> updateValueFactory As Func(Of TKey, TValue, TValue)
) As TValue

F#

[<ExtensionAttribute>]
static member AddOrUpdate : 
        [<NotNullAttribute>] dictionary : IDictionary<'TKey, 'TValue> * 
        key : 'TKey * 
        [<NotNullAttribute>][<InstantHandleAttribute>] addValueFactory : Func<'TKey, 'TValue> * 
        [<NotNullAttribute>][<InstantHandleAttribute>] updateValueFactory : Func<'TKey, 'TValue, 'TValue> -> 'TValue 

Parameters

 

dictionary
Type: System.Collections.Generic.IDictionary(TKey, TValue)
key
Type: TKey
The key to be added or whose value should be updated
addValueFactory
Type: System.Func(TKey, TValue)
The function used to generate a value for an absent key
updateValueFactory
Type: System.Func(TKey, TValue, TValue)
The function used to generate a new value for an existing key based on the key's existing value

Type Parameters

 

TKey
TValue

Return Value

Type: TValue
The new value for the key. This will be either be addValue (if the key was absent) or the result of updateValueFactory (if the key was present).

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IDictionary(TKey, TValue). When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

See Also

Reference

DictionaryExtensions Class
AddOrUpdate Overload
CodeJam.Collections Namespace

Clone this wiki locally