Coder Perfect

What is the purpose of the dollar symbol ($”string”)? [duplicate]


I was going through several C# exercises in a book when I came across one that stumped me. The output line is as follows, taken directly from the book:

Console.WriteLine($"\n\tYour result is {result}.");

The code executes correctly, and the double result appears as expected. However, because I couldn’t figure out why the $ was at the beginning of the text, I removed it, and the code now outputs the name of the array result instead of the contents. Unfortunately, the book does not explain why the $ is present.

Regarding string formatting and Console, I’ve been scouring the VB 2015 help and Google. Overload techniques for WriteLine are available. I’m not seeing anything that explains why things are the way they are. Any suggestions would be greatly appreciated.

Asked by BigTime

Solution #1

It’s Interpolated Strings, a new feature in C# 6.

An interpolated string expression constructs a string by substituting the included expressions with ToString representations of the expressions’ results.

Please go to MSDN for additional information on this.

Now think about it a little more. Why is this feature so beneficial?

For instance, suppose you had the class Point:

public class Point
    public int X { get; set; }

    public int Y { get; set; }

Create 2 instances:

var p1 = new Point { X = 5, Y = 10 };
var p2 = new Point { X = 7, Y = 3 };

You now want to display it on the screen. The two methods you generally employ are as follows:

Console.WriteLine("The area of interest is bounded by (" + p1.X + "," + p1.Y + ") and (" + p2.X + "," + p2.Y + ")");

As you can see, string concatenation makes the code difficult to comprehend and error-prone. You are free to use string. To make it nicer, use Format():

Console.WriteLine(string.Format("The area of interest is bounded by({0},{1}) and ({2},{3})", p1.X, p1.Y, p2.X, p2.Y));

This raises a new issue:

As a result, we should make use of the following new feature:

Console.WriteLine($"The area of interest is bounded by ({p1.X},{p1.Y}) and ({p2.X},{p2.Y})");

The placeholders are now maintained by the compiler, so you don’t have to worry about indexing the right argument because it’s right there in the string.

Please read the entire post on this blog.

Answered by Triet Doan

Solution #2

It resembles the String in appearance. Format() placeholders, except instead of an index, the curly brackets contain the expression itself. It shouldn’t come as a surprise that it resembles String. Because that’s all it is — syntactical sugar that the compiler treats as if it were a String. Behind the scenes, there’s Format().

The compiler now keeps track of the placeholders for you, so you don’t have to worry about indexing the correct argument because it’s right there in the string.

String interpolation in C# allows you to concatenate, format, and manipulate strings. This feature first appeared in C# 6.0. Objects and expressions can be used as part of the string interpolation operation when using string interpolation.

String interpolation syntax begins with a ‘$’ symbol, and expressions are defined within a bracket, as shown below.



The code below concatenates a string that includes an object, author, as part of the string interpolation.

string author = "Mohit";  
string hello = $"Hello {author} !";  
Console.WriteLine(hello);  // Hello Mohit !

C#/.NET Little Wonders: String Interpolation in C# 6 has further information.

Answered by Mohit Shrivastava

Post is based on