runtime: Immutable hash-based collections do not accept `null` values
I have spent two days chasing down an evil bug. I narrowed down the problem with this test case demonstrating different behavior for different collection classes that implement the same interface. Specifically Contains(null)
throws a ArgumentNullException
for ImmutableSortedSet
but not for Array
or the mutable SortedSet
.
using System.Collections.Generic;
using System.Collections.Immutable;
using Xunit;
public class SortedSetSpec
{
[Fact]
public void WTFNullCheck()
{
var data = new[] {"a", "b", "c", "d"};
SortedSet<string> ss = new SortedSet<string>(data);
ImmutableSortedSet<string> iss = ss.ToImmutableSortedSet();
// This passes
data.Contains(null).Should().BeFalse();
// This passes
ss.Contains(null).Should().BeFalse();
// This throws an exception
iss.Contains(null).Should().BeFalse();
}
}
Given that all classes Array / SortedSet and ImmutableSortedSet implement ICollection<T>
shouldn’t they have the same behavior on the Contains(null)
call?
The bug manifested itself when I data bound an ImmutableSortedSet
to the ItemsSource
property of a listbox.
<ListBox x:Name="ListBox" Margin="2"
ItemsSource="{Binding WorkflowTags}"
SelectedItem="{Binding SelectedTag}" >
</ListBox>
The problem is that at some point deep in the databinding code ListBox (aka Selector ) asks the collection Contains(null)
?? and then it crashes if I’ve databound an ImmutableSortedSet
.
So is this a bug with ImmutableCollections
or with WPF
or is it expected behavior and I should know better than to use ImmutableSortedSet
?
[EDIT] Add syntax highlighting by @karelz
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 15 (11 by maintainers)
Why close it? It’s obviously a bug in the immutable collections API. Liskov substition principle and all that. I should be able to swap any collection and it will have the same behaviour. What else can I do except report it. That I have a work around is not acknowledgment that there is no problem.
On Wed, 8 Feb 2017, 20:54 Santiago Fernandez Madero < notifications@github.com> wrote: