question about string

  • Thread starter Thread starter greenrob
  • Start date Start date
G

greenrob

hello,

I am just wondering what is the different by putting an extra "@" sign
in front of string delcaration ? Specifically :

string test = "hahaha";
string test1 = @"huhuuh";

What is the difference between test and test1 above ?

Thanks a bunch.

rgds
Rob
 
Hello,

The difference is : huhuuh does not contain 'a'. :p

Try "\\" and @"\\", you'll see the difference. @ defines a verbatim
string, where escape chars have no effect.


greenrob a écrit :
 
Hi,
hello,

I am just wondering what is the different by putting an extra "@" sign
in front of string delcaration ? Specifically :

string test = "hahaha";
string test1 = @"huhuuh";

What is the difference between test and test1 above ?

Thanks a bunch.

rgds
Rob

When you use a '@', the string is automatically escaped.

string test = "ha\\ha\\ha";
string test1 = @"ha\ha\ha";

http://msdn2.microsoft.com/en-us/library/ms228362.aspx
(scroll down a little)

HTH,
Laurent
 
greenrob said:
I am just wondering what is the different by putting an extra "@" sign
in front of string delcaration ? Specifically :

string test = "hahaha";
string test1 = @"huhuuh";

What is the difference between test and test1 above ?

Using a @ means the compiler treats the literal as a verbatim string
literal.

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

Note that the string itself will have no idea how it was originally
declared - it's a *compile-time* feature only.

Jon
 
Hi Rob,

@ in front of a string has two functions. The most commonly known is that
it will treat \ as a regular character instead of an escape character,
which is handy when writing file paths

@"C:\Folder\File.txt";

instead of

"C:\\Folder\\File.txt";

It will also let you write over several lines (ignoring line breaks)

@"
Line1
Line2
";

instead of

"\r\nLine1\r\nLine2\r\n";

or

"\r\n";
+ "Line1\r\n";
+ "Line2\r\n";

Which is handy for Xml etc
 

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

Back
Top