@@ -299,6 +299,83 @@ public static bool ContainsAllKeys<TKey, TValue>([DisallowNull] this IDictionary
299
299
300
300
return keys . All ( key => dictionary . ContainsKey ( key ) ) ;
301
301
}
302
+ public static IEnumerable < TKey > GetKeys < TCollection , TKey , TValue > ( this TCollection collection )
303
+ where TCollection : IEnumerable < KeyValuePair < TKey , TValue > >
304
+ {
305
+ return collection switch
306
+ {
307
+ IDictionary < TKey , TValue > dictionary => dictionary . Keys ,
308
+ IReadOnlyDictionary < TKey , TValue > readOnlyDictionary => readOnlyDictionary . Keys ,
309
+ _ => collection . Select ( kvp => kvp . Key ) . ToList ( ) ,
310
+ } ;
311
+ }
312
+ public static IEnumerable < TValue > GetValues < TCollection , TKey , TValue > ( this TCollection collection )
313
+ where TCollection : IEnumerable < KeyValuePair < TKey , TValue > >
314
+ {
315
+ return collection switch
316
+ {
317
+ IDictionary < TKey , TValue > dictionary => dictionary . Values ,
318
+ IReadOnlyDictionary < TKey , TValue > readOnlyDictionary => readOnlyDictionary . Values ,
319
+ _ => collection . Select ( kvp => kvp . Value ) . ToList ( ) ,
320
+ } ;
321
+ }
322
+ public static bool ContainsKey < TCollection , TKey , TValue > ( this TCollection collection , TKey key )
323
+ where TCollection : IEnumerable < KeyValuePair < TKey , TValue > >
324
+ {
325
+ return collection switch
326
+ {
327
+ IDictionary < TKey , TValue > dictionary => dictionary . ContainsKey ( key ) ,
328
+ IReadOnlyDictionary < TKey , TValue > readOnlyDictionary => readOnlyDictionary . ContainsKey ( key ) ,
329
+ _ => ContainsKey ( collection , key ) ,
330
+ } ;
302
331
332
+ static bool ContainsKey ( TCollection collection , TKey key )
333
+ {
334
+ var comparer = Comparer < TKey > . Default ;
335
+ return collection . Any ( kvp => comparer . Compare ( kvp . Key , key ) == 0 ) ;
336
+ }
337
+ }
338
+ public static bool TryGetValue < TCollection , TKey , TValue > ( this TCollection collection , TKey key , out TValue value )
339
+ where TCollection : IEnumerable < KeyValuePair < TKey , TValue > >
340
+ {
341
+ return collection switch
342
+ {
343
+ IDictionary < TKey , TValue > dictionary => dictionary . TryGetValue ( key , out value ) ,
344
+ IReadOnlyDictionary < TKey , TValue > readOnlyDictionary => readOnlyDictionary . TryGetValue ( key , out value ) ,
345
+ _ => TryGetValue ( collection , key , out value ) ,
346
+ } ;
347
+
348
+ static bool TryGetValue ( TCollection collection , TKey key , out TValue value )
349
+ {
350
+ var comparer = Comparer < TKey > . Default ;
303
351
352
+ foreach ( var kvp in collection )
353
+ {
354
+ if ( comparer . Compare ( kvp . Key , key ) == 0 )
355
+ {
356
+ value = kvp . Value ;
357
+ return true ;
358
+ }
359
+ }
360
+
361
+ value = default ;
362
+ return false ;
363
+ }
364
+ }
365
+ public static TValue GetValue < TCollection , TKey , TValue > ( this TCollection collection , TKey key )
366
+ where TCollection : IEnumerable < KeyValuePair < TKey , TValue > >
367
+ {
368
+ return collection switch
369
+ {
370
+ IDictionary < TKey , TValue > dictionary => dictionary [ key ] ,
371
+ IReadOnlyDictionary < TKey , TValue > readOnlyDictionary => readOnlyDictionary [ key ] ,
372
+ _ => GetValue ( collection , key ) ,
373
+ } ;
374
+
375
+ static TValue GetValue ( TCollection collection , TKey key )
376
+ {
377
+ var comparer = Comparer < TKey > . Default ;
378
+ return collection . First ( kvp => comparer . Compare ( kvp . Key , key ) == 0 ) . Value ;
379
+ }
380
+ }
304
381
}
0 commit comments