Reputation: 16512
simpleSound = new SoundPlayer(@"c:\Windows\Media\chimes.wav");
Why and what is the @ for?
Upvotes: 3
Views: 138
Reputation: 1073
It is a verbatim string. A verbatim string allows you to include special characters like \, " etc. without using the escape sequence.
Upvotes: 2
Reputation: 17566
"c:\\my\\file.txt"
could be done as @"c:\my\file.txt"
This means your \n and \t or whatever it is will also not be processed.
Upvotes: 0
Reputation: 19407
Defining the @ symbol prior to the assigning a string value will prevent the need for doubling the backslash (\) in C#. The at symbol (@) simply ignores escape characters.
Upvotes: 0
Reputation: 43531
Another usage of @
is that you can put it in front of a keyword, if you want to use a keyword as a variable name (even if it's not a good idea). Example:
public int @object = 1;
Upvotes: 0
Reputation: 37543
It identifies a string literal. It allows you to have the special character \
in the string without escaping it.
Upvotes: 2
Reputation: 532765
The @ sign before a string means to treat the backslash as a normal character rather than the start of a special character (such as newline).
Upvotes: 2
Reputation: 103770
It's a literal string. Instead of having to escape the "\" by putting two of them "\" the compiler interprets the string "as is".
Say you wanted to print out the following text to the screen: "Hello \t world"
.
If you were to just do Console.WriteLine("Hello \t world")
, then your output would be:
Hello world
notice the tab. That's because \t is interperted as a tab. If you use the literal though, like this:
Console.WriteLine(@"Hello \t world")
then your output would be:
"Hello \t world"
Upvotes: 8