newbie question, what's the '@' char before some strings?

A

anon

I'm just picking up C# here and there, so I have some basic gaps in my
C# knowledge. I'm actually working on it w/in .NET Compact Framework
because I am trying to create a PPC application, but this seemed more
like a basic C# question.

In some examples in a book I'm using (Yao/Durant's .NET CF Programming
with C#) there are strings with an '@'. What does that do?

For example, I'm looking at an example regarding SQL Server CE and
creating a local database:

private string strFile = @"My Documents\ourProduceCo.sdf";
private string strConn = "Data Source=" + @"My
Documents\ourProduceCo.sdf";

thanks
 
G

Guest

The "@" character tells the compiler to NOT expand escape sequences within
the string. Normal strings use escape sequences to represent non-printable
characters. Escape sequences start with a "\" character, and then one more
character. Normally, to get a "\" character in your string, you need to use
"\\", since the first "\" is considered the start of an escape sequence by
the compiler. If your string is a file/path name with lots of "\" characters,
and you don't need any escape sequences, using "@" before the string (i.e.
turning off escape sequences for this string) makes things a little easier.

Brian.
 
J

Jon Skeet [C# MVP]

I'm just picking up C# here and there, so I have some basic gaps in my
C# knowledge. I'm actually working on it w/in .NET Compact Framework
because I am trying to create a PPC application, but this seemed more
like a basic C# question.

In some examples in a book I'm using (Yao/Durant's .NET CF Programming
with C#) there are strings with an '@'. What does that do?

For example, I'm looking at an example regarding SQL Server CE and
creating a local database:

private string strFile = @"My Documents\ourProduceCo.sdf";
private string strConn = "Data Source=" + @"My
Documents\ourProduceCo.sdf";

They're verbatim string literals.

See http://www.pobox.com/~skeet/csharp/strings.html#literals
 

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