Attempts to remove and return the value with the specified key from the NamedMapTKey, TValue.
If the parameter is set to the
RemoveBehaviorOptions.DoNotReturnValue, the value is removed and is not returned via the
value parameter to optimize the method call performance.
Namespace: Soss.Client.Concurrent
Assembly: soss_namedcache (in soss_namedcache.dll) Version: 6.2.0.0
Syntax
Parameters
- key
- Type: TKey
The key of the element to remove and return. - removeBehavior
- Type: Soss.Client.ConcurrentRemoveBehavior
Defines whether the TryRemove(TKey, RemoveBehavior, TValue) method should return deleted key's value. - value
- Type: TValue
When this method returns, value contains the object removed from the NamedMapTKey, TValue or the default value of it if the operation failed.
Return Value
Type: Booleantrue if an object was removed successfully, otherwise it returns false.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | Thrown if key is a null reference. |
TimeoutException | Thrown when the maximum number of internal retries reached. |
Examples
The following example illustrates how to use the TryRemove method
using System; using System.Reflection; using Soss.Client; using Soss.Client.Concurrent; using System.Collections.Generic; using System.Diagnostics; class TryRemoveUsage { static void Main(string[] args) { NamedMap<int, string> map = new NamedMap<int, string>("myMap"); // Adding 3 elements into the map and check the count Debug.Assert(map.TryAdd(1, "A")); Debug.Assert(map.TryAdd(2, "B")); Debug.Assert(map.TryAdd(3, "C")); Debug.Assert(3 == map.ExecuteCount()); // Remove one of the existing elements from the map. Debug.Assert(map.TryRemove(3)); // Check to make sure the element was removed Debug.Assert(!map.ContainsKey(3)); string objVal = string.Empty; // Now change the default object removal behavior to return the deleted value Debug.Assert(map.TryRemove(2, RemoveBehavior.ReturnValue, out objVal)); // Verify that the correct value was returned: Debug.Assert("B" == objVal); // The third removal should now empty the map Debug.Assert(map.TryRemove(1)); if (map.ExecuteCount() == 0) Console.WriteLine("Map is now empty"); else Console.WriteLine("Map contains unexpected elements."); } }
See Also