3

I am using codecademy to learn C#, this is their code

using System;

namespace GettingInput
{
  class Program
  {
    static void Main()
    {
      Console.WriteLine("How old are you?");
      string input = Console.ReadLine();
      Console.WriteLine($"You are {input} years old!");
    }
  }
}

I did that but it told me unexpect '$' in line 11, i tried to copy paste the exact code because i thought maybe i did a small mistake i dont notice but still doesnt work, how do i fix this?

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • Hi, Console.WriteLine(==>$<=="You are {input} years old!"); ;) – Ostas Jun 05 '20 at 17:47
  • 15
    Make sure you're using at least C# 6 to use [string interpolation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) – itsme86 Jun 05 '20 at 17:47
  • 1
    woops didn't know about string interpolation. My bad – Ostas Jun 05 '20 at 17:48
  • 2
    In older versions of C# you would use [String.Format](https://learn.microsoft.com/en-us/dotnet/api/system.string.format#get-started-with-the-stringformat-method), and Console.WriteLine will do the same string formatting. The link from @itsme86 provides a good example of the two different approaches. – mcdon Jun 05 '20 at 18:10
  • 1
    CodeAcademy is using an older version of C#, which doesn't support the `$` string interpolation operator. As @mcdon said, you'll need to use `String.Format` or concatenate with `input.ToString()`. (`String.Format()` is a lot cleaner IMO.) – 3Dave Jun 05 '20 at 18:11

0 Answers0