为什么Console.WriteLine在Visual Studio Code中不起作用? [英] Why doesn't Console.WriteLine work in Visual Studio Code?

查看:699
本文介绍了为什么Console.WriteLine在Visual Studio Code中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio Code上安装了脚本和Coderunner.当我运行包含Console.WriteLine("Test")的简单程序时,看不到任何输出.该程序似乎已成功运行,并以代码0退出.

I have scriptcs and coderunner installed on Visual Studio Code. When I run a simple program that includes Console.WriteLine("Test") I don't see any output. The program seems to run successfully and exits with code 0.

有什么建议吗?

以下是所有代码,以防有人感兴趣:

Here's all the code in case anyone is interested:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Test");
    }
}

推荐答案

如果您只是尝试在没有项目等的情况下运行cs文件,则问题在于代码运行程序会将文件视为脚本.因此,实际上并没有像运行控制台应用程序那样调用main方法.

If you are just trying to run a cs file without a project etc then the problem is that code runner is treating the file as a script. As such the main method is actually not being invoked as it would be if running a console app.

因此,解决方案是公开您的main方法,并添加对Program.Main(null);的调用;在类定义之后.此解决方案不需要任何launch.json配置文件或配置更改.请注意,类定义在VS代码中确实显示为错误后对Program.Main的调用,但在代码运行程序中运行良好.请参见下面的代码块.

The solution therefore is to make your main method public and add a call to Program.Main(null); after the class definition. This solution does not require any launch.json config file or config changes. Note the call to Program.Main after the class definition does show as an error in VS code but it runs fine in code runner. See the code block below.

using System;
class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Test");
    }
}

Program.Main(null);

我在这里找到了答案: https://stackoverflow.com/a/46179597

I found the answer to this here: https://stackoverflow.com/a/46179597

这篇关于为什么Console.WriteLine在Visual Studio Code中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆