Skip to content

Implement ToString() on System.Collections.DictionaryEntry #59292

Closed

Description

@KirillOsenkov

DictionaryEntry still doesn't have a good display in the debugger or when printing it to a log.

Could we implement ToString() that returns $"{Key} = {Value}", like KeyValurPair<TKey,TValue> has?

Activity

  1. ghost added
    untriaged
    on Sep 18, 2021
  2. eiriktsarpalis commented on Sep 19, 2021

    @eiriktsarpalis
    Member

    So, should this be a change to how ToString() is implemented or would it make more sense to add a DebuggerDisplayAttribute?

  3. added
    enhancement
    and removed
    untriaged
    on Sep 19, 2021
  4. added this to the Future milestone on Sep 19, 2021
  5. KirillOsenkov commented on Sep 20, 2021

    @KirillOsenkov
    MemberAuthor

    I'd prefer ToString() because there are several advantages:

    • ToString() is useful in various other contexts, such as various text logs, format strings, interpolated strings, printf debugging, etc.
    • KeyValuePair<TKey, TValue> uses ToString()
    • compiler verifies correctness (vs. whatever you write in DebuggerDisplay is stringly typed)
    • you can place a breakpoint in ToString()
    • it's faster for the debugger (single eval call vs. interpreting the debugger display string)
  6. KirillOsenkov commented on Sep 20, 2021

    @KirillOsenkov
    MemberAuthor

    It's so frustrating when you see a useless type name in the logs for example, where it could have been meaningful and helpful to have the actual values.

  7. Joe4evr commented on Sep 20, 2021

    @Joe4evr
    Contributor
    • compiler verifies correctness (vs. whatever you write in DebuggerDisplay is stringly typed)

    Current preview can have compiler verification, tho:

    [DebuggerDisplay($"{{{nameof(DebugDisplay)},nq}}")]
    public class C
    {
        private string DebugDisplay => ...;
    }
  8. KirillOsenkov commented on Sep 20, 2021

    @KirillOsenkov
    MemberAuthor

    That's a good point, but unfortunately nobody writes it that way. Look at all existing DebuggerDisplay usages:
    https://source.dot.net/#System.Private.CoreLib/DebuggerDisplayAttribute.cs,761078d4519dc54e,references

    I just had to fix this recently because someone refactored code and forgot to update DebuggerDisplay:
    https://github.com/dotnet/msbuild/pull/6650/files

  9. Joe4evr commented on Sep 20, 2021

    @Joe4evr
    Contributor

    That's a good point, but unfortunately nobody writes it that way.

    Only because the feature is new and nobody's used it yet. But at the very least, any new applications of DebuggerDisplay could (and I believe should) make use of the new feature so that mistakes like the one you linked can be spotted at compile-time.

  10. krwq commented on Sep 27, 2021

    @krwq
    Member

    @KirillOsenkov would you expect to add any string escaping into ToString? I'd expect to make it consistent with perhaps Tuple or other similar classes

  11. KirillOsenkov commented on Sep 27, 2021

    @KirillOsenkov
    MemberAuthor

    sure, anything is fine, ValueTuple, KVP, as long as it’s something.

  12. KirillOsenkov commented on Sep 27, 2021

    @KirillOsenkov
    MemberAuthor

    curious how ToString looks for anonymous types and old Tuples

  13. eiriktsarpalis commented on Sep 27, 2021

    @eiriktsarpalis
    Member

    curious how ToString looks for anonymous types and old Tuples

    FWIW both prettyprint their contents.

  14. modified the milestones: Future, 7.0.0 on Oct 29, 2021
  15. self-assigned this
    on Feb 6, 2022
  16. jeffhandley commented on Feb 6, 2022

    @jeffhandley
    Member

    I'm tempted to just reuse the implementation for KeyValuePair.ToString(). It's available as a static internal:

            internal static string PairToString(object? key, object? value) =>
                string.Create(null, stackalloc char[256], $"[{key}, {value}]");

    There's no escaping or anything else fancy here, as anything like that would have performance hits for negligible value (only in odd cases). Here are the tests I have illustrating what the behavior would be:

        public class DictionaryEntry_Tests
        {
            public static IEnumerable<object?[]> ToString_Inputs()
            {
                yield return new object?[] { "key", "value", "[key, value]" };
                yield return new object?[] { 1, 2, "[1, 2]" };
                yield return new object?[] { typeof(object), (object?)null, "[System.Object, ]" };
                yield return new object?[] { "[key, value", "]]", "[[key, value, ]]]" };
                yield return new object?[] { new DictionaryEntry("key", "key"), new DictionaryEntry("value", "value"), "[[key, key], [value, value]]" };
            }
    
            [Theory]
            [MemberData(nameof(ToString_Inputs))]
            public void ToString_FormatsKeyValue(object key, object? value, string expected)
            {
                var entry = new DictionaryEntry(key, value);
                var result = entry.ToString();
    
                Assert.Equal(expected, result);
            }
        }

    Any objections to taking this approach? Preview: https://github.com/dotnet/runtime/compare/main...jeffhandley:jeffhandley/59292-de-tostring?expand=1

  17. danmoseley commented on Feb 6, 2022

    @danmoseley
    Contributor

    Does it truncate if key or value is huge? And do we want it to?

  18. jeffhandley commented on Feb 6, 2022

    @jeffhandley
    Member

    It does not do any truncation. Scanning through all places where we override ToString, I see lots of instances where we emit or build unbounded strings; I haven't found any where we truncate.

  19. ghost added
    in-pr
    on Feb 8, 2022
  20. ghost removed
    in-pr
    on Feb 9, 2022
  21. ghost locked as resolved and limited conversation to collaborators on Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Collectionsenhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions