1616
1717#region U S A G E S
1818
19+ using DomainCommonExtensions . DataTypeExtensions ;
1920using System ;
2021using System . Collections ;
2122using System . Collections . Generic ;
22- using DomainCommonExtensions . DataTypeExtensions ;
23+ using DomainCommonExtensions . CommonExtensions ;
24+ using DomainCommonExtensions . Utilities . Ensure ;
2325
2426#endregion
2527
@@ -62,6 +64,8 @@ public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TK
6264 public static Dictionary < TKey , TValue > RemoveKeys < TKey , TValue > ( this Dictionary < TKey , TValue > dict ,
6365 IEnumerable < TKey > keys )
6466 {
67+ DomainEnsure . IsNotNull ( dict , nameof ( dict ) ) ;
68+
6569 foreach ( var key in keys )
6670 if ( dict . ContainsKey ( key ) . IsTrue ( ) )
6771 dict . Remove ( key ) ;
@@ -77,13 +81,23 @@ public static Dictionary<TKey, TValue> RemoveKeys<TKey, TValue>(this Dictionary<
7781 /// <returns></returns>
7882 public static int IndexOf ( this IDictionary dictionary , object value )
7983 {
84+ if ( dictionary . IsNullOrEmpty ( ) ) return - 1 ;
85+
8086 for ( var i = 0 ; i < dictionary . Count ; ++ i )
8187 if ( dictionary [ i ] == value )
8288 return i ;
8389
8490 return - 1 ;
8591 }
8692
93+ /// <summary>
94+ /// Check if Dictionary is null or empty (with no values)
95+ /// </summary>
96+ /// <param name="dictionary">source dictionary</param>
97+ /// <returns></returns>
98+ public static bool IsNullOrEmpty ( this IDictionary dictionary )
99+ => dictionary . IsNull ( ) || dictionary . Count . IsZero ( ) ;
100+
87101 /// <summary>
88102 /// Get STRING or default value
89103 /// </summary>
@@ -153,5 +167,42 @@ public static bool GetBoolOrDefault(this Dictionary<string, string> dictionary,
153167
154168 return @default ;
155169 }
170+
171+ /// -------------------------------------------------------------------------------------------------
172+ /// <summary>
173+ /// An IDictionary<TK,TV> extension method that adds if not exist 'item' to source.
174+ /// </summary>
175+ /// <typeparam name="TKey">Type of the key.</typeparam>
176+ /// <typeparam name="TValue">Type of the value.</typeparam>
177+ /// <param name="source">The source to act on.</param>
178+ /// <param name="item">The item.</param>
179+ /// =================================================================================================
180+ public static void AddIfNotExist < TKey , TValue > ( this IDictionary < TKey , TValue > source , KeyValuePair < TKey , TValue > item )
181+ {
182+ DomainEnsure . IsNotNull ( source , nameof ( source ) ) ;
183+
184+ if ( source . ContainsKey ( item . Key ) ) return ;
185+
186+ source [ item . Key ] = item . Value ;
187+ }
188+
189+ /// -------------------------------------------------------------------------------------------------
190+ /// <summary>
191+ /// An IDictionary<TK,TV> extension method that adds if not exist 'item' to source.
192+ /// </summary>
193+ /// <typeparam name="TKey">Type of the key.</typeparam>
194+ /// <typeparam name="TValue">Type of the value.</typeparam>
195+ /// <param name="source">The source to act on.</param>
196+ /// <param name="key">Required. Search key.</param>
197+ /// <param name="value">.</param>
198+ /// =================================================================================================
199+ public static void AddIfNotExist < TKey , TValue > ( this IDictionary < TKey , TValue > source , TKey key , TValue value )
200+ {
201+ DomainEnsure . IsNotNull ( source , nameof ( source ) ) ;
202+
203+ if ( source . ContainsKey ( key ) ) return ;
204+
205+ source [ key ] = value ;
206+ }
156207 }
157208}
0 commit comments