1+ // ***********************************************************************
2+ // Assembly : RzR.Shared.Extensions.DomainCommonExtensions
3+ // Author : RzR
4+ // Created On : 2025-10-09 18:10
5+ //
6+ // Last Modified By : RzR
7+ // Last Modified On : 2025-10-09 18:05
8+ // ***********************************************************************
9+ // <copyright file="ConcurrentDictionaryExtensions.cs" company="RzR SOFT & TECH">
10+ // Copyright © RzR. All rights reserved.
11+ // </copyright>
12+ //
13+ // <summary>
14+ // </summary>
15+ // ***********************************************************************
16+
17+ #region U S A G E S
18+
19+ using System ;
20+ using System . Collections . Concurrent ;
21+ using System . Collections . Generic ;
22+ using System . Linq ;
23+
24+ #endregion
25+
26+ namespace DomainCommonExtensions . ArraysExtensions
27+ {
28+ /// -------------------------------------------------------------------------------------------------
29+ /// <summary>
30+ /// A concurrent dictionary extensions.
31+ /// </summary>
32+ /// =================================================================================================
33+ public static class ConcurrentDictionaryExtensions
34+ {
35+ /// -------------------------------------------------------------------------------------------------
36+ /// <summary>
37+ /// A ConcurrentDictionary<TKey,TValue> extension method that removes this object.
38+ /// </summary>
39+ /// <typeparam name="TKey">Type of the key.</typeparam>
40+ /// <typeparam name="TValue">Type of the value.</typeparam>
41+ /// <param name="dictionary">The dictionary to act on.</param>
42+ /// <param name="key">The key.</param>
43+ /// =================================================================================================
44+ public static void Remove < TKey , TValue > ( this ConcurrentDictionary < TKey , TValue > dictionary , TKey key )
45+ {
46+ dictionary . TryRemove ( key , out _ ) ;
47+ }
48+
49+ /// -------------------------------------------------------------------------------------------------
50+ /// <summary>
51+ /// A ConcurrentDictionary<TKey,TValue> extension method that removes this object.
52+ /// </summary>
53+ /// <typeparam name="TKey">Type of the key.</typeparam>
54+ /// <typeparam name="TValue">Type of the value.</typeparam>
55+ /// <param name="dictionary">The dictionary to act on.</param>
56+ /// <param name="keys">The keys.</param>
57+ /// =================================================================================================
58+ public static void Remove < TKey , TValue > ( this ConcurrentDictionary < TKey , TValue > dictionary ,
59+ IEnumerable < TKey > keys )
60+ {
61+ foreach ( var key in keys )
62+ dictionary . Remove ( key ) ;
63+ }
64+
65+ /// -------------------------------------------------------------------------------------------------
66+ /// <summary>
67+ /// A ConcurrentDictionary<TKey,TValue> extension method that removes the where.
68+ /// </summary>
69+ /// <typeparam name="TKey">Type of the key.</typeparam>
70+ /// <typeparam name="TValue">Type of the value.</typeparam>
71+ /// <param name="dictionary">The dictionary to act on.</param>
72+ /// <param name="predicate">The predicate.</param>
73+ /// =================================================================================================
74+ public static void RemoveWhere < TKey , TValue > ( this ConcurrentDictionary < TKey , TValue > dictionary ,
75+ Func < TValue , bool > predicate )
76+ {
77+ dictionary . Remove ( dictionary . Where ( kvp => predicate ( kvp . Value ) ) . Select ( kvp => kvp . Key ) ) ;
78+ }
79+
80+ /// -------------------------------------------------------------------------------------------------
81+ /// <summary>
82+ /// A ConcurrentDictionary<TKey,TValue> extension method that try remove.
83+ /// </summary>
84+ /// <typeparam name="TKey">Type of the key.</typeparam>
85+ /// <typeparam name="TValue">Type of the value.</typeparam>
86+ /// <param name="dictionary">The dictionary to act on.</param>
87+ /// <param name="key">The key.</param>
88+ /// =================================================================================================
89+ public static void TryRemove < TKey , TValue > ( this ConcurrentDictionary < TKey , TValue > dictionary , TKey key )
90+ {
91+ dictionary . TryRemove ( key , out _ ) ;
92+ }
93+
94+ /// -------------------------------------------------------------------------------------------------
95+ /// <summary>
96+ /// A ConcurrentDictionary<TKey,TValue> extension method that removes all.
97+ /// </summary>
98+ /// <typeparam name="TKey">Type of the key.</typeparam>
99+ /// <typeparam name="TValue">Type of the value.</typeparam>
100+ /// <param name="dictionary">The dictionary to act on.</param>
101+ /// <param name="keys">The keys.</param>
102+ /// =================================================================================================
103+ public static void RemoveAll < TKey , TValue > ( this ConcurrentDictionary < TKey , TValue > dictionary ,
104+ IEnumerable < TKey > keys )
105+ {
106+ foreach ( var key in keys )
107+ dictionary . TryRemove ( key , out _ ) ;
108+ }
109+
110+ /// -------------------------------------------------------------------------------------------------
111+ /// <summary>
112+ /// A ConcurrentDictionary<TKey,TValue> extension method that removes all.
113+ /// </summary>
114+ /// <typeparam name="TKey">Type of the key.</typeparam>
115+ /// <typeparam name="TValue">Type of the value.</typeparam>
116+ /// <param name="dictionary">The dictionary to act on.</param>
117+ /// <param name="predicate">The predicate.</param>
118+ /// =================================================================================================
119+ public static void RemoveAll < TKey , TValue > ( this ConcurrentDictionary < TKey , TValue > dictionary ,
120+ Func < TValue , bool > predicate )
121+ {
122+ var keys = dictionary . Where ( p => predicate ( p . Value ) ) . Select ( p => p . Key ) . ToArray ( ) ;
123+
124+ RemoveAll ( dictionary , keys ) ;
125+ }
126+
127+ /// -------------------------------------------------------------------------------------------------
128+ /// <summary>
129+ /// A ConcurrentDictionary<TKey,TValue> extension method that gets value or default.
130+ /// </summary>
131+ /// <typeparam name="TKey">Type of the key.</typeparam>
132+ /// <typeparam name="TValue">Type of the value.</typeparam>
133+ /// <param name="dictionary">The dictionary to act on.</param>
134+ /// <param name="key">The key.</param>
135+ /// <returns>
136+ /// The value or default.
137+ /// </returns>
138+ /// =================================================================================================
139+ public static TValue GetValueOrDefault < TKey , TValue > ( this ConcurrentDictionary < TKey , TValue > dictionary ,
140+ TKey key )
141+ {
142+ if ( dictionary . TryGetValue ( key , out var value ) )
143+ return value ;
144+
145+ return default ( TValue ) ;
146+ }
147+ }
148+ }
0 commit comments