Why does C# require escape ar @ characters for strings ?

  • Thread starter Thread starter YoYo Pa
  • Start date Start date
Y

YoYo Pa

Why does C# require an escape character (or the @ symbol placed in front of
the string) when a string variable contains some *special characters* - as
in the sample code below. Other languages let you put anything you want into
a string without escaping. What's the deal with C#?

string strFilePath = strRootPath + "SubFolder1\\SubFolder1\\" +
strFileNameToUse;

Thanks
 
Greets,

This is because C#, Java, C and C++ (languages loosely related in some
fashion) allow for special characters such as carriage returns, newlines,
tabs, etc. to be placed in the string by escaping it with the backslash '\'
symbol. In the case where you want a backslash to be embedded (as it is the
special "escape" sequence character) you need to use two backslashes.

The '@' symbol allows you to take the backslash at face value and not
have to escape the backslashes. The downside is that the special characters
then present a problem if you need them. For example:

Console.WriteLine("The\n");
Console.WriteLine(@"The\n");

In the first case, "The" will displayed on the console with a newline,
while in the latter case, "The\n" is displayed literally. Typically you
don't add those special characters to file names, so '@' is a nice shortcut.

Regards,

Joe
 
YoYo said:
Other languages let you
put anything you want into a string without escaping.

That's not true, and impossible at the same time.

For example, in VB.Net, it allows you to have slash characters in strings,
but what about double quotes? Those must be escaped, by typing the quote
twice in a row:

Dim foo As String
foo = "Now "" is the time"

There is no language that I know of that has a string indicator that doesn't
need to be escaped.


--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
 
<<<<That's not true, and impossible at the same time>>>>

on second thought, I can't think of any either.

I stand corrected...

Cheers!


Reginald Blue said:
YoYo said:
Other languages let you
put anything you want into a string without escaping.

That's not true, and impossible at the same time.

For example, in VB.Net, it allows you to have slash characters in strings,
but what about double quotes? Those must be escaped, by typing the quote
twice in a row:

Dim foo As String
foo = "Now "" is the time"

There is no language that I know of that has a string indicator that doesn't
need to be escaped.


--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
 
YoYo said:
<<<<That's not true, and impossible at the same time>>>>

I stand corrected...

I find it's easier to be corrected when sitting down. :-)

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
 
Back
Top