471,328 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

At or @ usage

I'm new to C#. What's the purpose of the @ ??? I can't find it
indexed in my few C# books. It seems to have something to do with
reading strings literally. That's just a guess. I notice if I place
it outside a path that is in quotes, I don't need the extra backslash.
For Example:

string stagger=@"c:\noteq.cmd";

is the same as

string stagger="c:\\noteq.cmd";

string stagger="c:\noteq.cmd";

without the @ gives an error.
Aug 7 '08 #1
6 1077
folderann wrote:
I'm new to C#. What's the purpose of the @ ??? I can't find it
indexed in my few C# books. It seems to have something to do with
reading strings literally. That's just a guess. I notice if I place
it outside a path that is in quotes, I don't need the extra backslash.
For Example:

string stagger=@"c:\noteq.cmd";

is the same as

string stagger="c:\\noteq.cmd";

string stagger="c:\noteq.cmd";

without the @ gives an error.
In this context @ means that \ is not escape character.

So you avoid writing the two backslashes to get one.

(you put a " in the text by doubling and you can have literal
newlines in the string also)

Arne
Aug 7 '08 #2
On Wed, 06 Aug 2008 22:42:05 -0400, folderann wrote:
I'm new to C#. What's the purpose of the @ ??? I can't find it indexed
http://msdn.microsoft.com/en-us/libr...90(VS.71).aspx

verbatim-string-literal:
@" verbatim -string-literal-charactersopt "

It removes the significance of the \ character, eg \n is a new line, \t
is a tab.
Aug 7 '08 #3
Just for completeness (from the post title, not the example given),
note that an @ prefix is also used (separately) to allow you to use an
otherwise reserved word (/keyword) as a regular name in C#. This is
usually only used when dealing with cross-language issues (different
languages have different keywords) - for example, you can refer to a
member/type/variable @default, when without the @ it would get
interpreted as a language keyword.

Marc
Aug 7 '08 #4
I use it to create long strings such as SQL queries.
Rather than concatenating the string together (at high cost) you can
use the @ to create a multi-line string.

string query = "SELECT * "
+ "FROM RandomTable";
or

string query = @"SELECT *
FROM RandomTable ";
Aug 7 '08 #5
On Aug 7, 3:06*pm, "cfps.Christian" <ge0193...@otc.eduwrote:
I use it to create long strings such as SQL queries.
Rather than concatenating the string together (at high cost) you can
use the @ to create a multi-line string.

string query = "SELECT * "
* * *+ "FROM RandomTable";
or

string query = @"SELECT *
* * *FROM RandomTable ";
There is no execution time cost to doing this in the first form. So
long as the strings are constant, they are concatenated by the
compiler rather than at execution time.
Even if it were performed at execution time, the cost of concatenating
strings is going to be just noise compared with the cost of actually
executing the query and waiting for the results.

Now there's a reasonable argument that the second form is easier to
edit etc, and that's all fine - but the performance argument is a
nonstarter.

Jon
Aug 7 '08 #6
A belated thanks to all. It was very enlightening. I deduced that
the @ took care of the identification of the escape sequence, but I
didn't understand any of it's other uses.

Thanks to all again. I got wrapped up in an other problema and forgot
about this post.

On Thu, 7 Aug 2008 07:15:47 -0700 (PDT), "Jon Skeet [C# MVP]"
<sk***@pobox.comwrote:
>On Aug 7, 3:06pm, "cfps.Christian" <ge0193...@otc.eduwrote:
>I use it to create long strings such as SQL queries.
Rather than concatenating the string together (at high cost) you can
use the @ to create a multi-line string.

string query = "SELECT * "
+ "FROM RandomTable";
or

string query = @"SELECT *
FROM RandomTable ";

There is no execution time cost to doing this in the first form. So
long as the strings are constant, they are concatenated by the
compiler rather than at execution time.
Even if it were performed at execution time, the cost of concatenating
strings is going to be just noise compared with the cost of actually
executing the query and waiting for the results.

Now there's a reasonable argument that the second form is easier to
edit etc, and that's all fine - but the performance argument is a
nonstarter.

Jon
Aug 11 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by rbt | last post: by
2 posts views Thread by tomvr | last post: by
26 posts views Thread by Bruno Jouhier [MVP] | last post: by
11 posts views Thread by Paulo Eduardo | last post: by
10 posts views Thread by rdemyan via AccessMonster.com | last post: by
3 posts views Thread by Sirisha | last post: by
1 post views Thread by spacecoyote | last post: by
1 post views Thread by sowmya.rangineni | last post: by
2 posts views Thread by jld | last post: by
reply views Thread by rosydwin | last post: by

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.