473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

what does the @ do to strings and paths

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
Feb 26 '07 #1
11 1274
>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.

Feb 26 '07 #2
Peted wrote:
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
Feb 26 '07 #3
<Petedwrote:
I see many c# code samples that relate to strings or using strings in
file paths, that have an @ sign.
See http://pobox.com/~skeet/csharp/strings.html#literals

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 26 '07 #4
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.

Feb 26 '07 #5
AA2e72E <AA*****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 26 '07 #6
On Mon, 26 Feb 2007 01:39:07 -0800, AA2e72E <AA*****@discussions.microsoft.com>
wrote:
>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.
Yes, you can.

It works great when you have a long SQL string and want to keep it within the
margins of an editor.

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
Feb 26 '07 #7
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.
Feb 26 '07 #8
AA2e72E <AA*****@discussions.microsoft.comwrote:
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";

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 26 '07 #9
Jon Skeet [C# MVP] wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 26 '07 #10
Göran Andersson <gu***@guffa.comwrote:
Jon Skeet [C# MVP] wrote:
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.
Indeed. For more information, see
http://pobox.com/~skeet/csharp/stringbuilder.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 26 '07 #11
Jon Skeet [C# MVP] wrote:
Göran Andersson <gu***@guffa.comwrote:
>Jon Skeet [C# MVP] wrote:
>>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.

Indeed. For more information, see
http://pobox.com/~skeet/csharp/stringbuilder.html
Here's a real life experience, where converting 125 kB of data into a
hex-dump using string concatenation resulted in copying 75 GB (!) of
string data:

http://www.codeproject.com/script/co...=1#xx1519247xx

:)

--
Göran Andersson
_____
http://www.guffa.com
Feb 26 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: David | last post by:
Hi all, hope someone can help me figure this one out. I do not know if they are related or not, but I have recently installed SP 6 for VB6. After that, I noticed that when I run my program, whether...
3
by: Brian van den Broek | last post by:
Hi all, I'm just starting to employ unit testing (I'm using doctest), and I am uncertain how to handle writing tests where the behaviour being tested is dependant on whether certain file paths...
34
by: Sunner Sun | last post by:
Hi! I have looked through the FAQ but found nothing about it. :-) It seems that this kind of macro is platform dependent, doesn't it? Thank you. Sunner Sun
6
by: Grant Schenck | last post by:
I can't seem to find a documentation describing this. Thanks! Grant Schenck
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
15
by: Sandra-24 | last post by:
Comparing file system paths as strings is very brittle. Is there a better way to test if two paths point to the same file or directory (and that will work across platforms?) Thanks, -Sandra
0
by: Steve Holden | last post by:
dudeja.rajat@gmail.com wrote: You could use one of the URL libraries. The os.path module is for dealing with the local filesystem. The absence of any exception from f =...
35
by: Stef Mientki | last post by:
hello, I (again) wonder what's the perfect way to store, OS-independent, filepaths ? I can think of something like: - use a relative path if drive is identical to the application (I'm still a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.