dotnet / dotnet/wpf

EnumerableCollectionView broken in WPF due to Enumerator changes in Dictionary

Open
#9,157 9 comments 1 reaction 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

https://github.com/dotnet/runtime/pull/82499 and then the follow up changes that introduced `GenericEmptyEnumerator` have completely broken `EnumerableCollectionView` in WPF for the case of initially empty dictionaries.

The issue here is that `EnumerableCollectionView` obtains an enumerator during construction and caches it.
When asked to check for changes it invokes `MoveNext` on the cached enumerator and expects and exception to be thrown if the underlying list/collection/dictionary was modified.

When the dictionary has a count of 0 during construction the enumerator will never throw that exception as it's and instance of `GenericEmptyEnumerator`.

@stephentoub

### Reproduction Steps

```
var test = new Dictionary();

Assert.That(CollectionViewSource.GetDefaultView(test).Cast().Count(), Is.EqualTo(0));

test.Add("TEST", "TEST");

Assert.That(CollectionViewSource.GetDefaultView(test).Cast().Count(), Is.EqualTo(1));
```

### Expected behavior

Count obtained from the view should be 1 after adding an item

### Actual behavior

Count obtained from the view is 0 after adding an item

### Regression?

Yes. Worked prior to the changes from the mentioned PR.

### Known Workarounds

_No response_

### Configuration

.NET 8 and later

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.