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

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
 
J

Joe Delekto

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
 
R

Reginald Blue

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]
 
Y

YoYo Pa

<<<<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]
 
R

Reginald Blue

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]
 

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