Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation - what does the @ sign inside the curly braces do?

Tags:

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?

like image 350
Thomas Avatar asked Aug 31 '18 08:08

Thomas


People also ask

What does ${} dollar sign and curly braces mean in a string in JavaScript?

It's used to reference a variable within string: let someVar = "World!" console. log(`Hello ${someVar}`); // Output is Hello World!

What does the ${} mean in JavaScript?

${} 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.

What does string interpolation do?

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.

What does ${} do in HTML?

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.


2 Answers

$"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.

like image 142
MakePeaceGreatAgain Avatar answered Sep 17 '22 13:09

MakePeaceGreatAgain


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.

like image 26
ChristianMurschall Avatar answered Sep 21 '22 13:09

ChristianMurschall