Why do people use an @ sign before a string?

A

Alan Silver

Hello,

I have noticed in a few bits of code that people sometimes do things
like...

SomeFunction(@"a text string");

What is the purpose of the @ sign? I have seen it most when passing
connection strings (admittedly in example code, as connection strings
would be better stored elsewhere in a real app), but I don't think it's
related to that.

Anyone care to enlighten me? TIA
 
A

Alan Silver

The @ can shorten typing, as it states "This string is a literal". Example of
two equivalent strings:

string URL = @"http://msnd.microsoft.com/newsgroups";
string URL = "http:////msdn.microsoft.com//newsgroups";

In the second string, every whack has to be escaped.

Ooh, ain't that clever!! I'm not sure why you need to double the / in
the second example though, is it used for something special? I thought
the backslash was, say for embedding a newline character.

How about embedding quotes, would this help?

Thanks for the reply.
***************************
Think Outside the Box!

When I grow big enough to see over the top, I'll try thinking!!
 
W

William F. Robertson, Jr.

Yes, you are correct. I am sure Gregory meant that. Those should have been
backspaces such as a unc path.

You were asking about escaping string.

string s = "He said, \"No!\"";
string s = @"He said, ""No!""";

Notice the double double quotes.

bill
 
M

Matt Berther

Hello Alan,

@ means to take the string literally...

Take for example a file path:

string file = "c:\temp\foo.xml";

This will not be valid, since \t is actually a tab character. So, you have
a choice of either doing this:

string file = "c:\\temp\\foo.xml";

or

string file = @"c:\temp\foo.xml";
 
A

Alan Silver

Yes, you are correct. I am sure Gregory meant that. Those should have been
backspaces such as a unc path.

You were asking about escaping string.

string s = "He said, \"No!\"";
string s = @"He said, ""No!""";

Notice the double double quotes.

But what do you gain in this case? You have substituted one escape
character for another. My first impression was that you could type a
string without escape characters at all. Your example doesn't give any
benefit for having the @.

Apart from string containing backslashes, is there any benefit of the @
notation?

Thanks for the reply
 
D

darrel

string file = "c:\temp\foo.xml";
This will not be valid, since \t is actually a tab character. So, you have
a choice of either doing this:

Is that in all of the .net languages or just some? I believe I am passing
all sorts of file paths with backslashes as strings without the @ in vb.net.
Maybe I just have been lucky and haven't stumbled on a 'slash caracter' pair
that's actually an escape of something?

-Darrel
 
A

Alan Silver

Thanks Matt, that's a good example.
Hello Alan,

@ means to take the string literally...

Take for example a file path:

string file = "c:\temp\foo.xml";

This will not be valid, since \t is actually a tab character. So, you
have a choice of either doing this:

string file = "c:\\temp\\foo.xml";

or

string file = @"c:\temp\foo.xml";
 
B

bruce barker

its a c# feature (i forget what language c# stole it from). vb does not use
\ as a quote char. beside making it easier to build filename straings, it
also allows the newline character so you can do:

string html =
@"<html>
<body>
</body>
</html>
";

-- bruce (sqlwork.com)


| > string file = "c:\temp\foo.xml";
| >
| > This will not be valid, since \t is actually a tab character. So, you
have
| > a choice of either doing this:
|
| Is that in all of the .net languages or just some? I believe I am passing
| all sorts of file paths with backslashes as strings without the @ in
vb.net.
| Maybe I just have been lucky and haven't stumbled on a 'slash caracter'
pair
| that's actually an escape of something?
|
| -Darrel
|
|
 
W

William F. Robertson, Jr.

Oh, you can also do this.

string script = @"
<script language=""javascript"">
function myFunction()
{
alert( 'hello world' );
}
";

or
string script = String.Format( @"
<script language=""javascript"">
function myFunction()
{
alert( document.all[""{0}""].value );
}
", myControl.ID );

It makes it more readable, it is purely a personal preference, so if you
don't want to use it, then don't. I use both with a '@' and without
depending on my usage and what I am going for.

string uncPath = "\\\\server1\\share\directory\\file.txt"
string uncPath = @\\server1\share\directory\file.txt

bill

Alan Silver said:
Yes, you are correct. I am sure Gregory meant that. Those should have been
backspaces such as a unc path.

You were asking about escaping string.

string s = "He said, \"No!\"";
string s = @"He said, ""No!""";

Notice the double double quotes.

But what do you gain in this case? You have substituted one escape
character for another. My first impression was that you could type a
string without escape characters at all. Your example doesn't give any
benefit for having the @.

Apart from string containing backslashes, is there any benefit of the @
notation?

Thanks for the reply
 
A

Alan Silver

Oh, you can also do this.

Now that's *really* clever!! Thanks for pointing it out.
string script = @"
<script language=""javascript"">
function myFunction()
{
alert( 'hello world' );
}
";

or
string script = String.Format( @"
<script language=""javascript"">
function myFunction()
{
alert( document.all[""{0}""].value );
}
", myControl.ID );

It makes it more readable, it is purely a personal preference, so if you
don't want to use it, then don't. I use both with a '@' and without
depending on my usage and what I am going for.

string uncPath = "\\\\server1\\share\directory\\file.txt"
string uncPath = @\\server1\share\directory\file.txt

bill
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top