From 62b6969eca77e73e6a84e1c086bbb08fc206890f Mon Sep 17 00:00:00 2001 From: Michael van der Horst Date: Thu, 2 Oct 2025 10:27:50 +0200 Subject: [PATCH] Refactor SynchronizedDictionary to return lists for Keys/Values Updated the Keys and Values properties in SynchronizedDictionary to return List and List respectively, instead of KeyCollection and ValueCollection. This was achieved using the ToList() method. This resolves a possible InvalidOperationException. --- SmartThreadPool/SynchronizedDictionary.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/SmartThreadPool/SynchronizedDictionary.cs b/SmartThreadPool/SynchronizedDictionary.cs index 0cce19f..9572a58 100644 --- a/SmartThreadPool/SynchronizedDictionary.cs +++ b/SmartThreadPool/SynchronizedDictionary.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; namespace Amib.Threading.Internal { @@ -45,7 +46,11 @@ public TValue this[TKey key] { lock (_lock) { - return _dictionary[key]; + if (_dictionary.TryGetValue(key, out TValue value)) + { + return value; + } + return default; } } set @@ -57,24 +62,24 @@ public TValue this[TKey key] } } - public Dictionary.KeyCollection Keys + public List Keys { get { lock (_lock) { - return _dictionary.Keys; + return _dictionary.Keys.ToList(); } } } - public Dictionary.ValueCollection Values + public List Values { get { lock (_lock) { - return _dictionary.Values; + return _dictionary.Values.ToList(); } } }