Skip to content

Iterate Through a Dictionary in C#: .NET 10, Keys/Values samples, honest benchmarks - #2270

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/65385-csharp-iterate-through-dictionary
Oct 5, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/65385-csharp-iterate-through-dictionary

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for https://code-maze.com/csharp-iterate-through-dictionary/ (collections-dictionary/IterateThroughDictionary only).

  • All three projects net6.0 to net10.0.
  • Packages, re-queried on NuGet 2026-10-04 (newest stable): BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1, MSTest.TestAdapter 4.4.1, MSTest.TestFramework 4.4.1, coverlet.collector 10.1.0. dotnet list package --vulnerable --include-transitive is clean.
  • New sample methods SubDictionaryUsingKeys, SubDictionaryUsingValues and SubDictionaryRemoveWhileIterating, each with a test.
  • WhenDictionaryUsesParallelEnumerable_ThenOutputsReqdResults could not fail (every assert sat inside an if comparing the same values); it now uses CollectionAssert.AreEquivalent over the four expected lines.
  • Benchmarks: the dead stores are fed to BenchmarkDotNet Consumer. The string concatenation benchmark the article describes is now in the repo; its parallel case accumulates per thread (ThreadLocal) and joins at the end instead of writing to one shared local from every thread, which lost most of the appends (10,000 entries: 11,584 of 98,890 characters). All three methods now return the full 98,890-character result.
  • File-scoped namespaces, ImplicitUsings on the test project, stray space inside an interpolation hole removed.

Build 0 warnings, 7 of 7 tests pass, SDK 10.0.302 / .NET 10.0.10.

…terating samples, honest benchmarks

- Retarget all three projects to net10.0; BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1, MSTest 4.4.1, coverlet.collector 10.1.0
- Add SubDictionaryUsingKeys, SubDictionaryUsingValues and SubDictionaryRemoveWhileIterating with tests
- Replace the parallel output test that could not fail with an order-insensitive assertion
- Benchmarks: consume results with BenchmarkDotNet's Consumer instead of dead stores; add the string concatenation benchmark with a per-thread accumulator in the parallel case instead of a racy shared local
- File-scoped namespaces, ImplicitUsings on the test project, stray space in an interpolation hole removed
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 8768f52 into CodeMazeBlog:main Oct 5, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant