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?

6
  • I can't reproduce this behavior on my VS2010, I get the new line in the immediate window. Commented May 19, 2010 at 19:56
  • Are there Immediate Window settings I'm not seeing? Commented May 19, 2010 at 20:01
  • Sorry I was just typing the name of the var which gives the correct output, I get the same behavior as you with var.ToString() Commented May 19, 2010 at 20:05
  • @Julien - I'm thinking I'm going to just do that. However there are additional undesirable things being output that I wasn't ready to address today. No matter! Commented May 19, 2010 at 20:09
  • @Julien is correct, if you type just ?var then you will get it right
    – volody
    Commented May 19, 2010 at 20:09

2 Answers 2

140

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

7
  • Thanks, works nicely. Why the question mark in the front of the variable though? It seems to work just fine without it.
    – Samik R
    Commented Dec 8, 2011 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. Commented Dec 8, 2011 at 18: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
    Commented Mar 21, 2014 at 16:42
  • 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
    Commented Sep 15, 2015 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
    Commented Apr 24, 2020 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.

1
  • 2
    I was hoping for some sort of "Escaping" Config Option that could be turned Off for Intermediate Window. Alas there is none! Commented May 19, 2010 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.