what does the @ do to strings and paths

J

Jonathan Woods

From MSDN
----------------
@-quoted string literals start with @ and are enclosed in double
quotation marks.
For example:
@"good morning" // a string literal
The advantage of @-quoting is that escape sequences are not processed,
which makes it easy to write, for example, a fully qualified file
name:
@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
To include a double quotation mark in an @-quoted string, double it:
@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.
 
E

Ebbe Kristensen

Peted said:
string path1 = @"mydir";
string path2 = @"\mydir";

what does the @ do ?

It disables the backslash as escape character. Without the @, the second
directory name should have been

string path2 = \\mydir;

Ebbe
 
P

Peted

I see many c# code samples that relate to strings or using strings in
file paths, that have an @ sign.

eg

string path1 = @"mydir";
string path2 = @"\mydir";


what does the @ do ?

Peted
 
G

Guest

I looked at the link; just wondered if you have views on code like this:

string myString = @"Line 1
Line 2
Line 3";

where \r\n is omitted but myString retains the linefeeds, as
MessageBox.Show(myString) confirms.
 
J

Jon Skeet [C# MVP]

AA2e72E said:
I looked at the link; just wondered if you have views on code like this:

string myString = @"Line 1
Line 2
Line 3";

where \r\n is omitted but myString retains the linefeeds, as
MessageBox.Show(myString) confirms.

Well, it can be very handy, but the main problem I have with it is
indentation - for the sake of reading it in the code, it's nice to have
the text indented appropriately, but often you want the initial text of
each line to be at the start of the line, without spaces or tabs.

Other than that, I don't have any major issues with it.
 
O

Otis Mukinfus

G

Guest

Thanks Otis.
That is exactly where I have used it, to construct long SQL statements
(without the clutter of continuation line symbols or contatenation as in VB).
I asked the question because I wondered whether I was incurring any penalties
in performance.
 
J

Jon Skeet [C# MVP]

AA2e72E said:
Thanks Otis.
That is exactly where I have used it, to construct long SQL statements
(without the clutter of continuation line symbols or contatenation as in VB).
I asked the question because I wondered whether I was incurring any penalties
in performance.

No, none whatsoever. On the other hand, you also wouldn't be incurring
any penalties for doing:

string x = "first line"
+"second line"
+"third line";

as that would be done at compile-time.

Where you *would* get penalties (very small, admittedly) would be to
write it as:

string x = "first line";
x += "second line";
x += "third line";
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Jon said:
Where you *would* get penalties (very small, admittedly) would be to
write it as:

string x = "first line";
x += "second line";
x += "third line";

Yes, the performance penalty is small in this case, compared to the time
it will take to make the database call.

It might be worth to mention, however, that you should be careful when
you concatenate strings that way, as it scales very badly. It should
only be used when the number of strings are known beforehand, as the
execution time increases exponentially to an increase in number of strings.
 
J

Jon Skeet [C# MVP]

Göran Andersson said:
Yes, the performance penalty is small in this case, compared to the time
it will take to make the database call.

It might be worth to mention, however, that you should be careful when
you concatenate strings that way, as it scales very badly. It should
only be used when the number of strings are known beforehand, as the
execution time increases exponentially to an increase in number of strings.

Indeed. For more information, see
http://pobox.com/~skeet/csharp/stringbuilder.html
 

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