Newlines in the Immediate Window

Visual StudioDebuggingVisual Studio-2010NewlineImmediate Window

Visual Studio Problem Overview


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?

Visual Studio Solutions


Solution 1 - Visual Studio

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

Solution 2 - Visual Studio

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBob KaufmanView Question on Stackoverflow
Solution 1 - Visual StudiodavesemView Answer on Stackoverflow
Solution 2 - Visual StudioJaredParView Answer on Stackoverflow