cohtml.DictionaryExtensions
Provides polyfill extension methods for dictionaries. These methods were officially introduced in .NET Standard 2.1 (Unity 2021.2+). This extension class ensures backward compatibility for projects running on Unity 2020.4 or older (.NET Standard 2.0).
Public Functions
Section titled “Public Functions”| Name | |
|---|---|
| bool | TryAdd< TKey, TValue >(this System.Collections.Generic.IDictionary< TKey, TValue > dictionary, TKey key, TValue value) Attempts to add the specified key and value to the dictionary. This is safer and slightly more performant than manually check because it clearly communicates intent and avoids throwing exceptions if the key already exists. |
| TValue | GetValueOrDefault< TKey, TValue >(this System.Collections.Generic.IDictionary< TKey, TValue > dictionary, TKey key, TValue defaultValue =default) =default Tries to get the value associated with the specified key in the dictionary. If the key is not found, it gracefully falls back to a provided default value instead of throwing a KeyNotFoundException. |
Public Functions Documentation
Section titled “Public Functions Documentation”function TryAdd< TKey, TValue >
Section titled “function TryAdd< TKey, TValue >”static bool TryAdd< TKey, TValue >( this System.Collections.Generic.IDictionary< TKey, TValue > dictionary, TKey key, TValue value)Attempts to add the specified key and value to the dictionary. This is safer and slightly more performant than manually check because it clearly communicates intent and avoids throwing exceptions if the key already exists.
Parameters:
- dictionary The dictionary to add the key/value pair to.
- key The key of the element to add.
- value The value of the element to add.
Exceptions:
- ArgumentNullException Thrown if the dictionary is null.
Template Parameters:
- TKey The type of the keys in the dictionary.
- TValue The type of the values in the dictionary.
Return: true if the key/value pair was added to the dictionary successfully; false if the key already exists.
function GetValueOrDefault< TKey, TValue >
Section titled “function GetValueOrDefault< TKey, TValue >”static TValue GetValueOrDefault< TKey, TValue >( this System.Collections.Generic.IDictionary< TKey, TValue > dictionary, TKey key, TValue defaultValue =default) =defaultTries to get the value associated with the specified key in the dictionary. If the key is not found, it gracefully falls back to a provided default value instead of throwing a KeyNotFoundException.
Parameters:
- dictionary The dictionary to search.
- key The key to locate.
- defaultValue The value to return if the key is missing. Defaults to the default value of TValue (e.g., null for objects, 0 for ints).
Exceptions:
- ArgumentNullException Thrown if the dictionary is null.
Template Parameters:
- TKey The type of the keys in the dictionary.
- TValue The type of the values in the dictionary.
Return: The value associated with the specified key, if the key is found; otherwise, the specified defaultValue .
© 2026 Coherent Labs. All rights reserved.