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?
It's used to reference a variable within string: let someVar = "World!" console. log(`Hello ${someVar}`); // Output is Hello World!
${} is a placeholder that is used in template literals. You can use any valid JavaScript expression such as variable, arithmetic operation, function call, and others inside ${}. The expression used inside ${} is executed at runtime, and its output is passed as a string to template literals.
String interpolation is a technique that enables you to insert expression values into literal strings. It is also known as variable substitution, variable interpolation, or variable expansion. It is a process of evaluating string literals containing one or more placeholders that get replaced by corresponding values.
The ${} syntax allows us to put an expression in it and it will produce the value, which in our case above is just a variable that holds a string! There is something to note here: if you wanted to add in values, like above, you do not need to use a Template Literal for the name variable.
$"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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With