Skip to content

Commit 957f83b

Browse files
committed
AvaloniaDictionary accept IDictionary as init collection
1 parent 9d564e4 commit 957f83b

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/Avalonia.Base/Collections/AvaloniaDictionary.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ public AvaloniaDictionary(int capacity)
3535
}
3636

3737
/// <summary>
38-
/// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class.
38+
/// Initializes a new instance of the <see cref="AvaloniaDictionary{TKey, TValue}"/> class using an IDictionary.
3939
/// </summary>
40-
public AvaloniaDictionary(Dictionary<TKey, TValue> initialDictionary)
40+
public AvaloniaDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer = null)
4141
{
42-
if (initialDictionary == null)
42+
if (dictionary != null)
4343
{
44-
throw new ArgumentNullException(nameof(initialDictionary));
44+
_inner = new Dictionary<TKey, TValue>(dictionary, comparer ?? EqualityComparer<TKey>.Default);
45+
}
46+
else
47+
{
48+
throw new ArgumentNullException(nameof(dictionary));
4549
}
46-
_inner = new Dictionary<TKey, TValue>(initialDictionary);
4750
}
4851

49-
5052
/// <summary>
5153
/// Occurs when the collection changes.
5254
/// </summary>

tests/Avalonia.Base.UnitTests/Collections/AvaloniaDictionaryTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.Specialized;
4+
45
using Avalonia.Collections;
56
using Avalonia.Data.Core;
7+
68
using Xunit;
79

810
namespace Avalonia.Base.UnitTests.Collections
@@ -158,29 +160,32 @@ public void Clearing_Collection_Should_Raise_PropertyChanged()
158160
}
159161

160162
[Fact]
161-
public void Constructor_Should_Throw_ArgumentNullException_When_Dictionary_Is_Null()
163+
public void Constructor_Should_Throw_ArgumentNullException_When_Collection_Is_Null()
162164
{
163165
Assert.Throws<ArgumentNullException>(() =>
164166
{
165-
var target = new AvaloniaDictionary<string, string>(null);
167+
var target = new AvaloniaDictionary<string, string>(null, null);
166168
});
167169
}
168170

171+
169172
[Fact]
170-
public void Constructor_Should_Initialize_With_Provided_Dictionary()
173+
public void Constructor_Should_Initialize_With_Provided_Collection()
171174
{
172-
var initialDictionary = new Dictionary<string, string>
175+
var initialCollection = new Dictionary<string, string>
173176
{
174177
{ "key1", "value1" },
175178
{ "key2", "value2" }
176179
};
177180

178-
var target = new AvaloniaDictionary<string, string>(initialDictionary);
181+
var target = new AvaloniaDictionary<string, string>(initialCollection, null);
179182

180183
Assert.Equal(2, target.Count);
181184
Assert.Equal("value1", target["key1"]);
182185
Assert.Equal("value2", target["key2"]);
183186
}
184187

188+
189+
185190
}
186191
}

0 commit comments

Comments
 (0)