String interpolation - what does the @ sign inside the curly braces do?
Consider:
string newline = "\r\n";
Console.WriteLine($"Hello without at{newline}how are you?");
Console.WriteLine($"Hello with at{@newline}how are you?");
The output of both lines is identical. The newline is always printed as a newline.
Hello without at
how are you?
Hello with at
how are you?
So when do I need the at sign inside the curly braces?
$"Hello { myValue }" is an interpolated string which was introduced in C#6. In your case this is equivalent to a call to String.Format("Hello {0}", myValue).
The verbatim (@) is needed when your variable has the same name as a keyword, which, as far as I know, newline is not. However the following would cause a compiler-error:
String.Format("Hello {0}", if)
whilst this won´t:
String.Format("Hello {0}", @if)
Here the verbatim tells the compiler that if is the name of a variable, not the if-keyword.
So you don´t need the verbatim in your case, because newline is not a keyword. Or in other words your code is equivalent to this:
Console.WriteLine("Hello with at{0}how are you?", @newline);
which is a valid (even though redundant) use of the verbatim.
For further information refer to the documentation about string-interpolation.
It's a redundant verbatim prefix. From the C# specification:
The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.
A use case would be if you would want to write a function like this:
private void print(string @string) => Console.WriteLine(@string);
Normally you would not be able to name an identifier string because it is a reserved keyword. The @ prefix enables you to do so.
string newline = "\r\n";
Console.WriteLine($"Hello without at{newline}how are you?");
Console.WriteLine($"Hello with at{@newline}how are you?");
The output of both lines is identical. The newline is always printed as a newline.
Hello without at
how are you?
Hello with at
how are you?
So when do I need the at sign inside the curly braces?
$"Hello { myValue }" is an interpolated string which was introduced in C#6. In your case this is equivalent to a call to String.Format("Hello {0}", myValue).
The verbatim (@) is needed when your variable has the same name as a keyword, which, as far as I know, newline is not. However the following would cause a compiler-error:
String.Format("Hello {0}", if)
whilst this won´t:
String.Format("Hello {0}", @if)
Here the verbatim tells the compiler that if is the name of a variable, not the if-keyword.
So you don´t need the verbatim in your case, because newline is not a keyword. Or in other words your code is equivalent to this:
Console.WriteLine("Hello with at{0}how are you?", @newline);
which is a valid (even though redundant) use of the verbatim.
For further information refer to the documentation about string-interpolation.
It's a redundant verbatim prefix. From the C# specification:
The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.
A use case would be if you would want to write a function like this:
private void print(string @string) => Console.WriteLine(@string);
Normally you would not be able to name an identifier string because it is a reserved keyword. The @ prefix enables you to do so.
Comments
Post a Comment