> For the complete documentation index, see [llms.txt](https://kinematicsoup.gitbook.io/reactor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinematicsoup.gitbook.io/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2.md).

# Class ksAtomicDictionary\<Key, Value>

Namespace: [KS.Reactor](/reactor/api-reference/ks.reactor.md)\
Assembly: KSCommon.dll

Wraps a Dictionary to make all read/write operations thread safe.

```csharp
public class ksAtomicDictionary<Key, Value>
```

#### Type Parameters

`Key`

`Value`

#### Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) ← [ksAtomicDictionary\<Key, Value>](/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2.md)

#### Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object\)), [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object-system-object\)), [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode), [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype), [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone), [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals), [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Properties

### Keys <a href="#ks_reactor_ksatomicdictionary_2_keys" id="ks_reactor_ksatomicdictionary_2_keys"></a>

Gets a collection of all keys.

```csharp
public Key[] Keys { get; }
```

#### Property Value

Key\[]

### Values <a href="#ks_reactor_ksatomicdictionary_2_values" id="ks_reactor_ksatomicdictionary_2_values"></a>

Gets a collection of all values.

```csharp
public Value[] Values { get; }
```

#### Property Value

Value\[]

### this\[Key] <a href="#ks_reactor_ksatomicdictionary_2_item__0" id="ks_reactor_ksatomicdictionary_2_item__0"></a>

Gets/Sets the value for a key.

```csharp
public Value this[Key k] { get; set; }
```

#### Property Value

Value

## Methods

### Clear() <a href="#ks_reactor_ksatomicdictionary_2_clear" id="ks_reactor_ksatomicdictionary_2_clear"></a>

Removes everything from the dictionary.

```csharp
public void Clear()
```

### ContainsKey(Key) <a href="#ks_reactor_ksatomicdictionary_2_containskey__0" id="ks_reactor_ksatomicdictionary_2_containskey__0"></a>

Checks if a key is in the dictionary.

```csharp
public bool ContainsKey(Key k)
```

#### Parameters

`k` Key

Key to check.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the key is in the dictionary.

### ForEach(ForEachCallback) <a href="#ks_reactor_ksatomicdictionary_2_foreach_ks_reactor_ksatomicdictionary__0__1__foreachcallback" id="ks_reactor_ksatomicdictionary_2_foreach_ks_reactor_ksatomicdictionary__0__1__foreachcallback"></a>

Call a delegate function on each pair in the dictionary.

```csharp
public void ForEach(ksAtomicDictionary<Key, Value>.ForEachCallback callback)
```

#### Parameters

`callback` [ksAtomicDictionary](/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2.md)\<Key, Value>.[ForEachCallback](/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2/ks.reactor.ksatomicdictionary-2.foreachcallback.md)

### ForEachThenClear(ForEachCallback) <a href="#ks_reactor_ksatomicdictionary_2_foreachthenclear_ks_reactor_ksatomicdictionary__0__1__foreachcallbac" id="ks_reactor_ksatomicdictionary_2_foreachthenclear_ks_reactor_ksatomicdictionary__0__1__foreachcallbac"></a>

Call a delegate function on each pair in the dictionary and then remove the pair if the callback returns true.

```csharp
public void ForEachThenClear(ksAtomicDictionary<Key, Value>.ForEachCallback callback)
```

#### Parameters

`callback` [ksAtomicDictionary](/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2.md)\<Key, Value>.[ForEachCallback](/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2/ks.reactor.ksatomicdictionary-2.foreachcallback.md)

### Get(Key, Value) <a href="#ks_reactor_ksatomicdictionary_2_get__0__1" id="ks_reactor_ksatomicdictionary_2_get__0__1"></a>

Gets the value for a key.

```csharp
public Value Get(Key k, Value defaultValue = default)
```

#### Parameters

`k` Key

Key to get value for.

`defaultValue` Value

Default value to return if the key is not in the dictionary.

#### Returns

Value

Value for the key.

### GetOrAddValue(Key, Factory, out Value) <a href="#ks_reactor_ksatomicdictionary_2_getoraddvalue__0_ks_reactor_ksatomicdictionary__0__1__factory__1" id="ks_reactor_ksatomicdictionary_2_getoraddvalue__0_ks_reactor_ksatomicdictionary__0__1__factory__1"></a>

Gets the value of a key from the dictionary. Creates a new value for the key by calling a factory and adds it to the dictionary if the key was not found.

```csharp
public bool GetOrAddValue(Key key, ksAtomicDictionary<Key, Value>.Factory factory, out Value value)
```

#### Parameters

`key` Key

Key to get value for.

`factory` [ksAtomicDictionary](/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2.md)\<Key, Value>.[Factory](/reactor/api-reference/ks.reactor/ks.reactor.ksatomicdictionary-2/ks.reactor.ksatomicdictionary-2.factory.md)

Factory for creating the value if the key was not found.

`value` Value

The value for the key

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the key was already in the dictionary, false if it was added with the factory.

### Remove(Key) <a href="#ks_reactor_ksatomicdictionary_2_remove__0" id="ks_reactor_ksatomicdictionary_2_remove__0"></a>

Removes a key/value pair from the dictionary.

```csharp
public bool Remove(Key k)
```

#### Parameters

`k` Key

Key to remove.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if they key was found and removed from the dictionary.

### Remove(Key, Value) <a href="#ks_reactor_ksatomicdictionary_2_remove__0__1" id="ks_reactor_ksatomicdictionary_2_remove__0__1"></a>

Removes a key/value pair from the dictionary if the value for the key matches the given value.

```csharp
public bool Remove(Key k, Value v)
```

#### Parameters

`k` Key

Key to remove.

`v` Value

The key/value pair will only be removed if the value for the key matches this.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the key/value pair was removed.

### TryAdd(Key, Value) <a href="#ks_reactor_ksatomicdictionary_2_tryadd__0__1" id="ks_reactor_ksatomicdictionary_2_tryadd__0__1"></a>

Tries to add the specified key and value to the dictionary.

```csharp
public bool TryAdd(Key k, Value value)
```

#### Parameters

`k` Key

Key to add.

`value` Value

Value to add.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the key/value pair was added to the dictionary successfully; false if the key already exists.

### TryGetValue(Key, out Value) <a href="#ks_reactor_ksatomicdictionary_2_trygetvalue__0__1" id="ks_reactor_ksatomicdictionary_2_trygetvalue__0__1"></a>

Tries to get the value for a key.

```csharp
public bool TryGetValue(Key k, out Value outValue)
```

#### Parameters

`k` Key

Key to get value for.

`outValue` Value

Out value.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if succesful.

### TryRemove(Key, out Value) <a href="#ks_reactor_ksatomicdictionary_2_tryremove__0__1" id="ks_reactor_ksatomicdictionary_2_tryremove__0__1"></a>

Tries to remove and return the value that has the specified key from the dictionary.

```csharp
public bool TryRemove(Key k, out Value outValue)
```

#### Parameters

`k` Key

Key to get value for.

`outValue` Value

The object removed from the dictionary, or the default value of the TValue type if key does not exist.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the object was removed successfully; otherwise, false.
