0

首先,我是 C# 新手(我的意思是非常新,我昨天学的。)。我能够在 Visual Studio 2019 中编译和运行我的代码。由于 Visual Studio 2019 在我的计算机上运行缓慢,我决定将 C# 扩展安装到 VS Code。但它不包括编译和运行代码的选项。所以我为 VS Code 安装了“Code Runner”扩展。(最终 C++ 和 C 编译在尝试找到 G++ 1 小时后工作,安装它并添加到路径)但 C# 仍然无法工作。当我按下工具栏(VS Code 顶部)上的“运行”图标时,它只是在 PowerShell 上运行:

scriptcs "c:\Users\ymzym\OneDrive\C# Projects\GitHub\Hello-World\Hello_World_C#.cs"

5秒后给我这个错误:

ERROR: Script compilation failed. [CompilationErrorException] error CS7088: Invalid 'Usings' value: 'System // 'System' library is required for terminating the program in the 'Exit' function.'.

我根据错误消息尝试了一切。另外我已经导入了系统库using System;,我尝试System.Windows.Forms.Application.ExitThread();从我的代码中删除。我尝试从一开始就编写整个程序,但没有改变。更奇怪的是只有scriptcs不能编译。Visual Studio 2019 可以。

有趣的是,当我用一个基本的 Hello World 程序替换整个代码时,它给了我另一个错误:

ERROR: Script compilation failed. [CompilationErrorException] c:\Users\ymzym\OneDrive\C# Projects\GitHub\Hello-World\Hello_World_C#.cs(1,1): error CS7021: You cannot declare namespace in script code

这是我的代码:

using System;
namespace HelloWorld {
    class Program {
        static void Greeting() {
            string version = "0.1.0";
            string build = "Build 01";
            Console.WriteLine("███████████████████████████████████████████████████████");
            Console.WriteLine("███████████▓▒░ Hello World in C# (CSharp) ░▒▓██████████");
            Console.WriteLine("███████████████████████████████████████████████████████");
            Console.WriteLine($"█████████████▓▒░ Version {version} {build} ░▒▓████████████");
            Console.WriteLine("███████████████████████████████████████████████████████");
        }
        static void Question() {
            Console.WriteLine("█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█");
            Console.WriteLine("█ [1] Hello World                                     █");
            Console.WriteLine("█ [2] Greeting                                        █");
            Console.WriteLine("█ [3] Exit                                            █");
            Console.WriteLine("█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█");
            Console.Write("Your choice: ");
        }
        static void Exit() {
            System.Windows.Forms.Application.ExitThread();
        }
        static void Main(string[] args) {
            string version = "0.1.0";
            string build = "Build 01";
            Console.Title = $"Hello World in C# (Csharp) - Version {version} {build}";
            Greeting();
            while (true) {
                Question();
                try {
                    string choice = Console.ReadLine();
                    if (choice == "1") {
                        Console.WriteLine("Hello World!");
                    }
                    else if (choice == "2") {
                        Console.Write("Type your name: ");
                        string name = Console.ReadLine();
                        Console.WriteLine($"Hello, {name}");
                    }
                    else if (choice == "3") {
                        Exit();
                    }
                    else {
                        Console.WriteLine($"ERROR: There is no function defined for '{choice}', please type 1, 2 or 3 which are showed above.");
                    }
                }
                catch (Exception) {
                    Console.WriteLine("ERROR: Your choice must be a number. Numbers are shown above.");
                } 
            }
        }
    }
}

这是仍然会导致错误的简单 Hello World 程序:

namespace HelloWorld {
    class Hello {         
        static void Main(string[] args) {
            System.Console.WriteLine("Hello World!");
        }
    }
}
4

1 回答 1

1

您正在使用scriptcs运行您的 cs 程序。看起来您正在尝试创建控制台应用程序,但将其作为脚本运行(dotnet run用于运行 c# 应用程序)。这可能是因为在 VS Code 中只打开了 c# 文件。脚本很强大,但它们与 c# 项目不同。在项目中使用 c# 可能是您在 Visual Studio 中所做的事情,这就是它在那里工作的原因(以及它允许​​您使用命名空间的原因)。要在 VS Code 中打开项目,您需要打开解决方案文件 (.sln) 或包含项目文件的整个文件夹。

您可以选择为 ac# 脚本编写代码并遵循这些规则,或者为某种类型的 ac# 项目编写代码(控制台应用程序看起来像您想要的项目类型)。您的问题是您将两者混合在一起。

于 2021-06-09T15:08:16.330 回答