2

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.

LPLN
  • 475
  • 1
  • 6
  • 20
Roly Poly
  • 489
  • 1
  • 9
  • 19
  • 2
    Why are you using `csc`? The correct way to build from the command line is to use `msbuild`. If you want an easier time, use .NET Core and you can build with `dotnet`. It doesn't even require installing Visual Studio. – Herohtar Dec 14 '19 at 06:24
  • thanks for informing me about ```msbuild``` and ```dotnet``` – Roly Poly Dec 14 '19 at 06:32
  • Does this answer your question? [Error CS1056: Unexpected character '$' running the msbuild on a tfs continuous integration process](https://stackoverflow.com/questions/42932577/error-cs1056-unexpected-character-running-the-msbuild-on-a-tfs-continuous-i) – rfcdejong Jul 18 '22 at 14:38

2 Answers2

-1

This just happened to me using DOT Net Fiddle C# playground. Then I noticed, that to the left the active compiler was .NET 4.

When I switched the dropdown to say I wanted .NET 6 (by the way, this erased my practice code, but Ctrl-Z restored it), then the $ interpolated string worked exactly as expected.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
StackOverflowUser
  • 945
  • 12
  • 10
-3

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.

Roly Poly
  • 489
  • 1
  • 9
  • 19
  • 1
    You could also just use .NET Core. The SDK can be installed by itself without Visual Studio, and the command line operations are very simple. – Herohtar Dec 14 '19 at 06:29