Add index to OrderedDictionary debugger display #120885
Draft
+68
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds index information to the debugger display of
OrderedDictionary<TKey, TValue>
to show the position of each element in the ordered collection. The debugger display format is now[INDEX/KEY, VALUE]
instead of just[KEY, VALUE]
.Problem
Since .NET 9, the debugger display for dictionaries changed from
[INDEX, KEY/VALUE]
to[KEY, VALUE]
, using the genericIDictionaryDebugView
. While this works well for standard dictionaries where order is not significant,OrderedDictionary
maintains insertion order and supports index-based access as a key feature. Losing the index information in the debugger makes it difficult to understand the element positions and order.Solution
Created a custom
OrderedDictionaryDebugView<TKey, TValue>
debug proxy that displays items with both index and key information:The new format clearly shows:
Changes
OrderedDictionaryDebugView.cs
: New custom debugger type proxy withDebugViewOrderedDictionaryItem<TKey, TValue>
struct using[DebuggerDisplay("{Value}", Name = "[{Index}/{Key}]")]
OrderedDictionary.cs
: UpdatedDebuggerTypeProxy
attribute fromIDictionaryDebugView<,>
toOrderedDictionaryDebugView<,>
System.Collections.csproj
: Added new file to compilationDebugView.Tests.cs
: Added test cases validating the[INDEX/KEY]
format for both empty and populated OrderedDictionariesTesting
Fixes #issue_number
Original prompt
Fixes #118590
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.