【发布时间】:2019-12-14 06:08:37
【问题描述】:
我正在摆弄我学到的一些新的 C# 代码。我真的是从今天开始学习的,所以这是一个非常新的问题。
基本上,我学会了将 $ 放在字符串前面,以便使用 CodeCademy 中的花括号和变量名进行格式化。在课程中,我学会了像这样使用 C# 的 $ 标记:
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);
}
}
}
现在我已经结束了在 Visual Studio Code 中编写自己的代码来练习的过程。不幸的是,当我使用 $ 标记时,我的 csc 编译器告诉我:
text.cs(13,28): error CS1056: Unexpected character '$'
这是导致错误的代码:
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);
}
}
我的预期输出是“我在一年级就学会了字母表。这很容易。” $ 启动字符串插值,变量填充它。但是我得到一个错误。
我尝试用谷歌搜索错误并阅读the pages that show up。他们没有希望,因为他们建议安装一些东西,这对于看起来像语法错误的东西来说似乎有点过分了。
该代码在 Visual Studio 2019 中确实有效,因此我认为错误来自使用 csc.exe 作为我的编译器,“此编译器作为 Microsoft (R) .NET Framework 的一部分提供,但仅支持语言版本直到 C# 5,它不再是最新版本。”
在这种情况下,我似乎必须升级,但升级到什么?
在终端中替换 csc.exe 有什么好处?我只想编译可执行文件并运行它们,因为我很乐意在 Visual Studio Code 中编写代码。
【问题讨论】:
-
你为什么使用
csc?从命令行构建的正确方法是使用msbuild。如果您想要更轻松的时间,请使用 .NET Core,您可以使用dotnet进行构建。它甚至不需要安装 Visual Studio。 -
感谢您告知我有关
msbuild和dotnet的信息