Why does @"""" result in "\""" ??

J

Johann Blake

I posted a related problem today. The problem is this:

string str1 = @"""";

When I execute this code (even in a bare bones application), in the
IDE it returns "\"""

Why? Even in the immediate window it return this. Anywhere where I
attempt to use two apostrophes, it inserts a backslash. Even a
function in one method that saves its string to a file shows that the
backslash is really present because it also gets saved to the file. I
thought maybe just the IDE was having a "display" problem, but that
backslash really is in the string.

I used the repair feature of the VS.NET feature but it didn't help.
This morning I ran an Update on my complete system from Microsoft's
Update Website and I am certain that it has done something to the .NET
framework.

Any help is appreciated.

Thanks,
Johann Blake
 
G

Greg Ewing [MVP]

Johann, those two strings are equal. @"""" =="\""

The @ specifies that the string should be taken literally. The double "'s
indicate that that " shouldn't be considered a string terminator but instead
should be printed. The \" is an escape sequence which again specifies that
the " should not be considered as a string terminator but a ". When I run
the following code I get a single " as I should. In the IDE I do see the
"\"" but that's just how the IDE displays an escaped ". You should not see
the \ in any of the output. If you can reproduce this in a simple program
like I included below feel free to post it so we can check it out.

static void Main(string[] args)
{
string str1 = @"""";
Console.WriteLine(str1);
Console.Read();
}
 
I

Ignacio Machin

Hi Johann,

That's the expected behavior, you see there is two forms of write a string
consisting of a " character:
@"""" or "\""

these are two forms of saying the same thing.

the IDE is always display such expression in the same way, using the escape
sequence.
if you do a Console.Write( ) you will see the same result on both cases.


Hope this help,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top