diff --git a/src/Avalonia.Base/Utilities/SmallDictionary.cs b/src/Avalonia.Base/Utilities/SmallDictionary.cs index ea6769c1ba7..fbb22058a60 100644 --- a/src/Avalonia.Base/Utilities/SmallDictionary.cs +++ b/src/Avalonia.Base/Utilities/SmallDictionary.cs @@ -77,9 +77,17 @@ void SetCore(TKey key, TValue value, bool overwrite) } else { - // We have a single element, upgrade to array + // We have a single element, check if we should update the value. + var data = (TKey)_data; + if (data == key && overwrite) + { + _value = value; + + return; + } + // If we do not replace it, upgrade to array. arr = new KeyValuePair[6]; - arr[0] = new KeyValuePair((TKey)_data, _value); + arr[0] = new KeyValuePair(data, _value); arr[1] = new KeyValuePair(key, value); _data = arr; _value = default; diff --git a/tests/Avalonia.Base.UnitTests/Utilities/InlineDictionaryTests.cs b/tests/Avalonia.Base.UnitTests/Utilities/InlineDictionaryTests.cs index af47d2726a8..760d56fb58b 100644 --- a/tests/Avalonia.Base.UnitTests/Utilities/InlineDictionaryTests.cs +++ b/tests/Avalonia.Base.UnitTests/Utilities/InlineDictionaryTests.cs @@ -8,6 +8,17 @@ namespace Avalonia.Base.UnitTests.Utilities; public class InlineDictionaryTests { + [Fact] + public void Set_Twice_With_Single_Item_Works() + { + var dic = new InlineDictionary(); + dic["foo"] = 1; + Assert.Equal(1, dic["foo"]); + + dic["foo"] = 2; + Assert.Equal(2, dic["foo"]); + } + [Fact] public void Enumeration_After_Add_With_Internal_Array_Works() {