1

this might be a trivial question for the experienced programmers, however I came across this line of code and want to understand what it does. please see the code below where we are adding the in the entries list and also incrementing the count as well.

 public class Journal
 {
    private readonly List<string> entries = new List<string>();

    private static int count = 0;

    public int AddEntry(string text)
    {
        entries.Add($"{++count}: {text}"); // my question is about this line of code
        return count;
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
}

what is the purpose of doller sign in the mentioned line.

Haley Mueller
  • 487
  • 4
  • 16
hussian
  • 399
  • 6
  • 19
  • 1
    This is [string interpolation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) – Tor Aug 17 '18 at 09:12
  • this is string interpolation => https://learn.microsoft.com/en-US/dotnet/csharp/language-reference/tokens/interpolated – OrcusZ Aug 17 '18 at 09:12

1 Answers1

3

it is used for string interpolation,

with it you can write something like:

Console.WriteLine($"Test, this is your message: {message}");

instead of using the older command and syntax:

Console.WriteLine(String.Format("Test, this is your message: {0}", message));
Davide Piras
  • 43,984
  • 10
  • 98
  • 147