-->![C Generate Key Not In Dictionary C Generate Key Not In Dictionary](https://i.ytimg.com/vi/bUki7BKty9c/maxresdefault.jpg)
Definition
Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist. Returns the new value, or the existing value if the key already exists.
Overloads
![C Generate Key Not In Dictionary C Generate Key Not In Dictionary](https://i.ytimg.com/vi/bUki7BKty9c/maxresdefault.jpg)
GetOrAdd(TKey, Func<TKey,TValue>) | Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function if the key does not already exist. Returns the new value, or the existing value if the key exists. |
GetOrAdd(TKey, TValue) | Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist. Returns the new value, or the existing value if the key exists. |
GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) | Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function and an argument if the key does not already exist, or returns the existing value if the key exists. |
Examples
Key Generator: A key generator (keygen) is a cryptographic tool used to generate product keys, which are unique alpha-numeric sequences that tell an installer program that the user that initiated the install owns a license of the software. A key generator tries to generate a correct product key that allows the installation of the software to. Generating the Random Key in c# The RandomNumber class defined in the.NET Framework class library provides functionality to generate random key. Create the static class with name KeyGenerator. Declare the static readonly three char arrays. A Dictionary stores Key-Value pairs where the key must be unique. Before adding a KeyValuePair into a dictionary, check that the key does not exist using the ContainsKey method. Use the TryGetValue method to get the value of a key to avoid possible runtime exceptions. Python program to create a dictionary from a string Dictionary in python is a very useful data structure and at many times we see problems regarding converting a string to a dictionary. So, let us discuss how we can tackle this problem.
The following example shows how to call the GetOrAdd method:
Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function if the key does not already exist. Returns the new value, or the existing value if the key exists.
Parameters
- valueFactory
- Func<TKey,TValue>
The function used to generate a value for the key.
Returns
The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.
Exceptions
key
or valueFactory
is null
.The dictionary contains too many elements. How to generate ssl.
Remarks
For modifications and write operations to the dictionary, ConcurrentDictionary<TKey,TValue> uses fine-grained locking to ensure thread safety. (Read operations on the dictionary are performed in a lock-free manner.) However, the
valueFactory
delegate is called outside the locks to avoid the problems that can arise from executing unknown code under a lock. Therefore, GetOrAdd is not atomic with regards to all other operations on the ConcurrentDictionary<TKey,TValue> class.Since a key/value can be inserted by another thread while
valueFactory
is generating a value, you cannot trust that just because valueFactory
executed, its produced value will be inserted into the dictionary and returned. If you call GetOrAdd simultaneously on different threads, valueFactory
may be called multiple times, but only one key/value pair will be added to the dictionary.The return value depends on the presence of the key in the dictionary and whether a key/value is inserted by another thread after GetOrAdd is called but before
valueFactory
generates a value:Scenario | Return value |
---|---|
The key is already in the dictionary. | The existing value is returned. |
The key is not in the dictionary. valueFactory generates a value. On rechecking for the key, no key is found. | The key/value is inserted into the dictionary, and the value is returned. |
The key is not in the dictionary. valueFactory generates a value. While valueFactory is generating the value, a different thread inserts a value for the key. After valueFactory executes and upon rechecking for the key, the key inserted by the other thread is found. | The value inserted by the other thread is returned. |
See also
Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist. Returns the new value, or the existing value if the key exists.
Parameters
- value
- TValue
The value to be added, if the key does not already exist.
Returns
The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.
Exceptions
key
is null
.The dictionary contains too many elements.
See also
C Generate Key Not In Dictionary Google
GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg)
Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function and an argument if the key does not already exist, or returns the existing value if the key exists.
Type Parameters
- TArg
The type of an argument to pass into
valueFactory
.Parameters
- valueFactory
- Func<TKey,TArg,TValue>
The function used to generate a value for the key.
- factoryArgument
- TArg
An argument value to pass into
valueFactory
.Returns
The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.
Exceptions
key
is a null
reference (Nothing in Visual Basic).The dictionary contains too many elements.
Remarks
For modifications and write operations to the dictionary, ConcurrentDictionary<TKey,TValue> uses fine-grained locking to ensure thread safety. (Read operations on the dictionary are performed in a lock-free manner.) However, the
valueFactory
delegate is called outside the locks to avoid the problems that can arise from executing unknown code under a lock. Therefore, GetOrAdd is not atomic with regards to all other operations on the ConcurrentDictionary<TKey,TValue> class.Since a key/value can be inserted by another thread while
valueFactory
is generating a value, you cannot trust that just because valueFactory
executed, its produced value will be inserted into the dictionary and returned. If you call GetOrAdd simultaneously on different threads, valueFactory
may be called multiple times, but only one key/value pair will be added to the dictionary.The return value depends on the presence of the key in the dictionary and whether a key/value is inserted by another thread after GetOrAdd is called but before
valueFactory
generates a value:Scenario | Return value |
---|---|
The key is already in the dictionary. | The existing value is returned. |
The key is not in the dictionary. valueFactory generates a value. On rechecking for the key, no key is found. | The key/value is inserted into the dictionary, and the value is returned. |
The key is not in the dictionary. valueFactory generates a value. While valueFactory is generating the value, a different thread inserts a value for the key. After valueFactory executes and upon rechecking for the key, the key inserted by the other thread is found. | The value inserted by the other thread is returned. |