Self-documenting expressions in interpolated/formatted strings #4884
Replies: 3 comments 4 replies
-
I don't think "print debugging" should be favorized. |
Beta Was this translation helpful? Give feedback.
-
You can get reasonably close to this now by using an anonymous object. Performance should be fine outside of any tight loop scenarios:
Which prints: |
Beta Was this translation helpful? Give feedback.
-
With C# 10 using a custom interpolated string handler and |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What
One can extend syntax of interpolated strings with a
=
specifier. When present,=
would make the expression in curly braces part of the resulting string.In simple words, instead of writing:
One could write:
Why
This significantly eases debugging when logging with
Console.WriteLine
,logger.LogDebug
, VS tracepoints, etc.When adding a trace, one automatically gets a sensible log message that includes context. One doesn't need to come up with a lengthy message template and type it out.
More examples
Also...
This idea is borrowed from Python 3.8+:
[1] https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging
[2] https://bugs.python.org/issue36817
Beta Was this translation helpful? Give feedback.
All reactions