57

Using Visual Studio 2010 Professional, I have a ToString() method that looks like this:

public override string ToString()
{
    return "something" + "\n" + "something";
}

Because there are several "something"'s and each is long, I'd like to see

something
something

Sadly, I'm seeing

"something\nsomething"

Is there a way to get what I want?

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107

2 Answers2

139

Actually there is a way. You can use format specifiers in the immediate window to change the format of the display. If you have a string with carriage returns and linefeeds in it ("\r\n") you can follow the print request with the 'no quotes' format specifier.

In the immediate window type:

?MyObj.ToString(),nq

and the \r\n will cause newlines in the immediate window.

For more info on format specifiers see: http://msdn.microsoft.com/en-us/library/e514eeby.aspx

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
davesem
  • 1,406
  • 1
  • 9
  • 2
  • Thanks, works nicely. Why the question mark in the front of the variable though? It seems to work just fine without it. – Samik R Dec 08 '11 at 17:57
  • 8
    @Samik R - The question mark is required in Visual BASIC and is essentially ignored in C#. In BASIC, "?" is shorthand for the console PRINT statement. – Bob Kaufman Dec 08 '11 at 18:09
  • This is fantastic stuff. Thanks a lot. I wish things like these were documented at one place.. – Punit Vora Apr 13 '12 at 16:09
  • 2
    Wow wow wow, you just saved me yet another round of changing a hundred `\"` to `"` in the XML I was dumping to the Immediate Window. I can't begin to tell you how many small headaches you have just eliminated from my life. – RobertB Mar 21 '14 at 16:42
  • Darn! format specifiers apparently don't work in the JavaScript immediate window. – Pete Alvin Mar 11 '15 at 23:07
  • 1
    Just as a quickie, `System.Diagnostics.Debug.WriteLine("Some\r\nEscaped\r\nString")` also works as a solution to this; I've always thought it's because the immediate window is displaying the entity passed (and a string's entity includes escaped characters) vs WriteLine which is actually displaying the characters that result from evaluating the string in the current context.... – tobriand Sep 15 '15 at 13:41
  • 1
    It does not seem to work within Unity3D debugging, it says `Node not supported` when using `,nq` option. Would someone know why ? – StackHola Apr 24 '20 at 10:00
-4

Unfortunately no there is not. What's happening here is an artifact of the design of the debugger APIs.

The component responsible for processing the ToString() call is the expression evaluator. It's the data source for the majority of the debugger windows (watch, locals, immediate, etc ...).

For every window but the immediate the value is displayed on a single line. Displaying a multiline string on a single line doesn't make much sense. Hence the expression evaluator makes the string slightly more displayable by escaping newline characters into a displayable version.

This technique works pretty well for the locals and watch window. But in the immediate window where it makes more sense to display the multiline value it makes a lot less sense. Unfortunately the expression evaluator doesn't know the context of where it's data will be displayed and hence does the safe operation which is to escape the newlines.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    I was hoping for some sort of "Escaping" Config Option that could be turned Off for Intermediate Window. Alas there is none! – Bob Kaufman May 19 '10 at 20:40