You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#新手使用VS Code的Code Runner扩展时scriptcs持续编译报错的问题求助

解决VS Code中用Code Runner运行C#项目报错的问题

嘿,刚入坑C#就折腾VS Code切换开发环境,够拼的!我一眼就看出你遇到的问题根源了——你用错工具啦!

问题根源

scriptcs是专门用来运行C#脚本文件的工具,它不是为编译和运行带namespace、class的完整C#项目设计的。这就是为什么你的代码在Visual Studio 2019里能正常跑,但用scriptcs就报错:

  • 报错CS7088:脚本里的using语句后面不能加注释,而完整项目允许这种写法
  • 报错CS7021:脚本不允许声明namespace,它是直接执行代码片段的,不需要类和命名空间包裹

正确的解决方法(两种方案)

方案1:用VS Code的C#扩展原生运行完整项目(推荐)

其实你完全不需要Code Runner,C#扩展本身就支持完整项目的编译和运行,只要你装了.NET SDK:

  1. 先验证是否安装了.NET SDK:打开PowerShell,输入dotnet --version,如果能显示版本号就没问题,没装的话直接搜.NET SDK下载安装就行
  2. 在VS Code里打开你的整个项目文件夹(注意是包含.csproj文件的那个文件夹,不是单个.cs文件)
  3. VS Code会自动检测到C#项目,提示你生成launch.json和tasks.json配置文件,点击"是"跟着引导走就行
  4. 配置完成后,按F5或者点击顶部的运行按钮,就能正常运行你的项目了

方案2:修改Code Runner配置,调用dotnet命令运行

如果你非要用Code Runner,那得把它的执行命令从scriptcs改成dotnet run:

  1. 打开VS Code设置(快捷键Ctrl+,)
  2. 搜索Code Runner: Executor Map,点击"编辑 in settings.json"
  3. 找到C#对应的配置行,把原来的:
    "csharp": "scriptcs $fullFileName",
    
    修改成:
    "csharp": "dotnet run --project $dir",
    
    (如果是单个C#文件,也可以改成dotnet run $fullFileName,但完整项目推荐用--project $dir)
  4. 保存设置后,再点击Code Runner的运行按钮,就能正常编译运行了

如果你想试试C#脚本(scriptcs)

要是你好奇C#脚本怎么写,把你的代码改成脚本形式就行——去掉namespace、class,直接写方法和执行逻辑:

using System;

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("███████████████████████████████████████████████████████");
}

void Question()
{
    Console.WriteLine("█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█");
    Console.WriteLine("█ [1] Hello World █");
    Console.WriteLine("█ [2] Greeting █");
    Console.WriteLine("█ [3] Exit █");
    Console.WriteLine("█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█");
    Console.Write("Your choice: ");
}

void Exit()
{
    Console.WriteLine("Bye!");
    Environment.Exit(0);
}

// 主逻辑直接执行
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.");
    }
}

这个脚本就能用scriptcs正常运行啦~

内容的提问来源于stack exchange,提问作者Yılmaz Alpaslan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 18:48:11