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");
    }
}
SSteve
  • 10,550
  • 5
  • 46
  • 72
Matt West
  • 1,376
  • 1
  • 12
  • 18
  • Have you tried switching to the Debug window? – maccettura Apr 05 '17 at 16:52
  • There's a debug console window, there's no output there either, at least not in code runner. – Matt West Apr 05 '17 at 16:55
  • If you try `Debug.WriteLine("Test")` do you get something? @MattWest – I.B Apr 05 '17 at 16:58
  • 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#. – Matt West Apr 05 '17 at 17:43
  • @CNuts No, it errors out. Debug does not exist in the current context. – Matt West Apr 05 '17 at 17:45
  • 1
    @MattWest You need to add `using System.Diagnostics;` – I.B Apr 05 '17 at 17:48
  • @CNuts - Thanks, added that using. Still didn't work unfortunately. The code ran but no output. – Matt West Apr 05 '17 at 17:49

7 Answers7

22

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

Changing it from:

  "console": "internalConsole",

To:

  "console": "externalTerminal",

fixed it for me.

Tim
  • 623
  • 5
  • 12
  • 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. – Matt West Dec 13 '17 at 19:11
  • 6
    BTW: You can also use 'integratedTerminal' if you don't want to have a separate console (terminal) window. Here is the official documentation: https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window – Gregg Miskelly Feb 10 '18 at 19:14
  • 1
    As of 4/16/2018 this solution is no longer working. Thanks, Microsoft. – Matt West Apr 16 '18 at 19:49
  • perfect answer, the omnisharp docs from @GreggMiskelly should be added ;) – markfknight Jan 13 '20 at 22:05
  • 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. – Tore Aurstad Apr 23 '22 at 19:44
8

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

Kevin
  • 96
  • 1
  • 2
0

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.

  • 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! – Matt West Apr 05 '17 at 17:42
0

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

jonadv
  • 434
  • 6
  • 16
-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();
    }
}
mIhAwk
  • 119
  • 1
  • 7
  • 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! – Matt West Apr 05 '17 at 17:40
-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.

Kobrajunior
  • 65
  • 1
  • 6
  • 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. – Matt West Apr 05 '17 at 17:41
-3

You need to do this

Console.WriteLine("Hello");
string name = Console.ReadLine();
Jesse
  • 3,522
  • 6
  • 25
  • 40