PC Review


Reply
Thread Tools Rate Thread

what does the @ do to strings and paths

 
 
Jonathan Woods
Guest
Posts: n/a
 
      26th Feb 2007
>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.

 
Reply With Quote
 
 
 
 
Ebbe Kristensen
Guest
Posts: n/a
 
      26th Feb 2007
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


 
Reply With Quote
 
Peted
Guest
Posts: n/a
 
      26th Feb 2007
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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      26th Feb 2007
<Peted> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?QUEyZTcyRQ==?=
Guest
Posts: n/a
 
      26th Feb 2007
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.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      26th Feb 2007
AA2e72E <(E-Mail Removed)> 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.


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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Otis Mukinfus
Guest
Posts: n/a
 
      26th Feb 2007
On Mon, 26 Feb 2007 01:39:07 -0800, AA2e72E <(E-Mail Removed)>
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
 
Reply With Quote
 
=?Utf-8?B?QUEyZTcyRQ==?=
Guest
Posts: n/a
 
      26th Feb 2007
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.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      26th Feb 2007
AA2e72E <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      26th Feb 2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it simply that c# converts all strings coming in to double and strings going out to single? AAaron123 Microsoft C# .NET 3 23rd Feb 2009 01:08 PM
Looking for function to resolve VirtualStore paths to physical paths Scott Windows Vista General Discussion 9 8th Jul 2007 08:07 PM
Looking for function to resolve VirtualStore paths to physical paths Scott Windows Vista File Management 9 8th Jul 2007 08:07 PM
Make Word displays strings of text, not strings of code =?Utf-8?B?WGVybw==?= Microsoft Word Document Management 2 9th Dec 2004 10:35 AM
Link Paths changing to UNC paths Gail Microsoft Excel Misc 0 13th Apr 2004 07:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:58 PM.