简体   繁体   中英

Why am I getting "error CS1056: Unexpected character '$'" putting a $ mark here? A problem with my compiler?

I'm in the midst of fiddling around with some new C# code I learned. I literally started learning today so this is a very newb question.

Basically, I learned to put a $ in front of a string to enable formatting using curly braces and variable names from CodeCademy. In the lesson I learned to use C#'s $ marker like this:

using System;

namespace StoryTime
{
  class Program
  {
    static void Main(string[] args)
    {
      // Declare the variables
      string beginning = "Once upon a time,";
      string middle = "The kid climbed a tree";
      string end = "Everyone lived happily ever after.";

      // Interpolate the string and the variables
      string story = $"{beginning} {middle}. {end}";

      // Print the story to the console 
      Console.WriteLine(story);
    }
  }
}

Now I'm over in Visual Studio Code writing my own code to practice. Unfortunately, when I used the $ marker, my csc compiler told me:

text.cs(13,28): error CS1056: Unexpected character '$'

Here is the code that caused the error:


namespace Textperiment
{
    class Program
    {
        static void Main()
        {
            string a = "alphabet";
            string b = "learn";
            string c = "easy";

            string d = $"I {b}ed the {a} in 1st grade. It was {c}.";
            Console.WriteLine(d);
            Console.WriteLine(d.ToUpper);
        }
    }

My expected output is "I learned the alphabet in 1st grade. It was easy." $ initiates string interpolation, the variables fill it in. But instead I get an error.

I've tried Googling the error and reading the pages that show up . They aren't promising as they suggest installing stuff, which seems overkill for what looks like a syntax error.

The code did work in Visual Studio 2019 so I believe the error is coming from using csc.exe as my compiler, which "This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version."

In which case it does seem I have to upgrade, but to what?

What does a good job of replacing csc.exe in the terminal? I just wanna compile executables and run 'em since I'm happy writing code in Visual Studio Code.

As stated here

The problem can be fixed installing a Nuget package Microsoft.Net.Compilers. Below is the link of my highlighted answer: Project builds fine with Visual Studio but fails from the command line

That feature is a syntactic sugar for C#6, try to install the latest version of the framework 4.6.2 https://www.microsoft.com/en-us/download/details.aspx?id=53345

Then go to your Project properties and change on the Application option on Target framework to point to the latest. You don't need to change your code to replace the string interpolation with string.Format method to fix it. If you are still getting this error, is because, the compiler that is running your build is not the latest version of C#, try to add the Microsoft.Net.Compilers, from Nuget and compile again, that should resolve the issue. If you want to avoid to install this package, try to open your .csproj and take a look on the ToolsVersion.that should be pointing to the version 12, then change it to 14, but make sure you have installed the latest version of the MSBuild from https://www.microsoft.com/en-us/download/details.aspx?id=48159 or go to C:\\Program Files (x86)\\MSBuild\\14.0\\Bin, there you should have this folder with the csc.exe compiler. If even then that doesn't resolve the issue, then try to follow this steps https://msdn.microsoft.com/en-us/library/bb383985.aspx .

In my experience I solved this problem in 3 different ways:

1- just getting the package from Nuget

2- installing Microsoft Build Tools 2015 on the tfs server

3- The sledgehammer and last options but for me the best because you don't need to deal with the dependency on nuget, is installing the visual studio version on the tfs server where you run the process.

Hope this helps

So far the best solution I've found is to install Visual Studio 2019. There, the code runs exactly as I expect it to. I don't have to create an executable file either. But I still wish the csc.exe compiler would just work instead of throwing the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM