Why doesn't Console.WriteLine work in Visual Studio Code?
Asked Answered
L

7

20

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.

Any suggestions?

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");
    }
}
Lenardlenci answered 5/4, 2017 at 16:49 Comment(7)
Have you tried switching to the Debug window?Flower
There's a debug console window, there's no output there either, at least not in code runner.Lenardlenci
If you try Debug.WriteLine("Test") do you get something? @MattWestRonni
Hi all, note this is Visual Studio Code, not the Visual Studio IDE. The goal I'm shooting for is to execute the code in the Code Runner extension. There is an "output" window in that editor. If I run a JavaScript console.log there, I see the output I'd expect, but it doesn't seem to work in C#.Lenardlenci
@CNuts No, it errors out. Debug does not exist in the current context.Lenardlenci
@MattWest You need to add using System.Diagnostics;Ronni
@CNuts - Thanks, added that using. Still didn't work unfortunately. The code ran but no output.Lenardlenci
W
9

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.

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);

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

Wick answered 30/11, 2018 at 18:13 Comment(0)
M
23

In launch.json there should be a field called 'console':

Changing it from:

  "console": "internalConsole",

To:

  "console": "externalTerminal",

fixed it for me.

Microsporangium answered 12/12, 2017 at 17:32 Comment(5)
As of 12/13/17 I didn't even need to do that. I'm on basically a new machine now compared to when I wrote the question. All I did was install Chocolatey ScriptCS and the VS Code plugin ScriptCSRunner, which I'd done last time as well. Then I wrote up a quick script, hit run and it worked! Not sure what changed but the problem appears to have been fixed in a patch somewhere along the line. Best answer just for letting me know it was working now.Lenardlenci
BTW: You can also use 'integratedTerminal' if you don't want to have a separate console (terminal) window. Here is the official documentation: github.com/OmniSharp/omnisharp-vscode/blob/master/…Regurgitation
As of 4/16/2018 this solution is no longer working. Thanks, Microsoft.Lenardlenci
perfect answer, the omnisharp docs from @GreggMiskelly should be added ;)Reactive
Using an internal console sometimes crashes when I test it, such as doing "Console.Clear()"; Anybody know any reason for this and a list of do and don't regarding Console methods and internal console in VsCode.Shaffer
W
9

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.

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);

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

Wick answered 30/11, 2018 at 18:13 Comment(0)
S
1

It will show your output if you will press ctrl+F5. You will get the output in console window. Another solution, if you will write Console.ReadLine(); after console.writeline, it will remain open console window until you will not press any key.

Shenitashenk answered 5/4, 2017 at 17:1 Comment(1)
I'm trying to run the code in VS Code, rather than Visual Studio IDE. ctrl+F5 triggers a task runner in that editor. Thanks though!Lenardlenci
U
0

An alternative would be to use Debug.WriteLine("test") instead, it writes to the immediate window. Needs using System.Diagnostics

Uird answered 11/2, 2022 at 11:16 Comment(0)
C
-1

You need to add any one of following line code

Console.Readline()
Console.Read()
Console.ReadKey()

example:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Test");
        Console.ReadLine();
    }
}
Carminecarmita answered 5/4, 2017 at 17:4 Comment(1)
That works if I run it in the console window, but I'm trying to get it to run within VS Code, similar to how JavaScript's console.log works in VS Code using the Coderunner extension. Thanks though!Lenardlenci
A
-2

The code did compile and then run but it wen't very fast and the console application closed itself after the execution. To prevent this from happening you need to add another method like:

  • Console.Read();

or

  • Console.ReadLine();

(The Line stands for linefeed, that means that the cursor moves to the next line to the left).

This way, the application will close if you press the 'enter' key on your keyboard.

Abroms answered 5/4, 2017 at 17:17 Comment(1)
That works if I run it in the console window, but I'm trying to get it to run within VS Code, similar to how JavaScript's console.log works in VS Code using the Coderunner extension.Lenardlenci
G
-3

You need to do this

Console.WriteLine("Hello");
string name = Console.ReadLine();
Grammatical answered 26/6, 2018 at 7:18 Comment(1)
Duplicate to mlhAwk's answer, do not suggest what is suggested already.Arabel

© 2022 - 2024 — McMap. All rights reserved.