citykid
citykid

Reputation: 11112

Why does Visual Studio Debugger remove \n in display?

I expected the debugger display of variable a in the following code to be {11\n22} it however is {1122}:

class A
{
    public string Text;
    public override string ToString()
    {
        return this.Text;
    }
}

A a = new A();
a.Text = "11\n22";

The debugger shows in the variables window:

display string of the object  "{1122}"    // why not "{11\n22}" ?
a.Text                        "11\n22"
a.ToString()                  "11\n22"

Tested with VS2012 and VS2010. I never realized this before. Anybody knows WHY the display string omits the \n character?

even adding [DebuggerDisplay("Text")] gives the same result.

Upvotes: 3

Views: 1198

Answers (3)

Ant P
Ant P

Reputation: 25231

The \n is not omitted - it is evaluated. Try inserting \" somewhere in the string and you will see that a double-quote appears in the helper window. Similarly, if you enter a into the immediate window, you will see the following:

a
{"11
22"}
    Text: "11\"\n\\2\"2"

It only appears to be omitted because the helper window you're looking at squishes everything into one line.

I imagine that this is evaluated because this is effectively a summary of the object, so what it's showing you is the printed value of a.ToString() - Visual Studio is (correctly) interpreting \n as a newline.

Upvotes: 0

Coding child
Coding child

Reputation: 155

Try to pin it and you will be see what you need.

Upvotes: 4

Parimal Raj
Parimal Raj

Reputation: 20585

It seems like Visual Studio is showing the Object.ToString() removing all the whitespaces.

But if you copy the text & paste it onto Notepad it shows the string with all the whitespaces.

Visual studio seems to designed/coded that way!

Upvotes: 2

Related Questions