1

I'm trying to run a simple Hello World code in VSC 1.13.1.

using System;

public class Hello1
{
    public static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

It successfully completes execution but doesnt produce any output ie - Hello, World!

Any help please!

Using Code Runner.

SSteve
  • 10,550
  • 5
  • 46
  • 72
ShdwKnght333
  • 300
  • 4
  • 23
  • 1
    Did you see a flash of console window popping up and disappearing right away? If so, I think you can add another statement below Console.WriteLine(), which is Console.ReadLine(); – Ryan L Jul 10 '17 at 04:33

5 Answers5

2

Add Console.ReadKey() , so the output will be there until the key is pressed

   public static void Main()
   {
     Console.WriteLine("Hello, World!");
     Console.ReadKey();
   }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
2

There currently seems to be a problem with VSCode (or the C# extension). Other StackOverflow answers show that this didn't work a few years ago, then it started working, and now (2018) it doesn't work once again.

I'm having the same problem. The workaround I found is this: If I run my code in the debugger (Debug > Start Debugging), it shows the output. But if I just run it normally, it doesn't. That's the only workaround I've found or read about that works for me.

Todd Bradley
  • 139
  • 1
  • 7
1

Another option is to start the project with ctrl + F5 instead of just F5, it will keep the terminal open until you press a key as well

How to keep the console window open in Visual C++?

Alexandra
  • 423
  • 5
  • 10
0

It have come to my attention that Using Code Runner, at this time, indeed produces some inconsistent behavior for JS as well.
However, on a first look you have not installed the proper supporting addons (which may be the cause for your particular problem): Code Runner requires : "To run C# script, you need to install scriptcs" Which is said to use Chocolatey - package manager for Windows.

For the moment, disabling the extension, restarting VSCode and then enabling it - fixed the current common problem for JS (not always producing an output). Which may be preceding with C#, as well.
NB! Make sure you save your files manually before you run them using Code Run

I will test this today and add the results.

Test Results: After installing scriptcs and Chocolatey your HelloWorld.cs file can run, BUT will not produce result.The reason: the code is treated as a script. Meaning that there is nothing invoking the Main() method. Meaning, you have to do it. Example:

using System;
public class Hello
{
     public static void Main()
    {
        var message = "hiiii inside => Works"   ;
        Console.WriteLine(message);
        HW();
    }

    public static void HW(){
        Console.WriteLine("Hello, World!");
        // the ReadLine will not work because it is only one way solution : ouptut only
        // var a = Console.ReadLine();
        // Console.WriteLine("key pressed: " + a + "Doesn't work");
    }
}

Hello.Main();
//can be used like this as well
var message= "Hey helloooo outside WORKS";
Console.WriteLine(message);

Use Ctrl+Alt+N to start and Ctrl+Alt+M to stop(if hung). And it will hung, if you use Console.ReadLine(); - check(uncomment) the example code in the HW() method.

P.S. Namespaces can't be used in scripts - will be the error message you get, if you try to use them with Code Runner

Community
  • 1
  • 1
Vasil
  • 142
  • 2
  • 8
0

Well, Code Runner is a tool to run C# script. It will treat the main method as a normal method and will not call it.

You should just write the Main method:

System.Console.WriteLine("hello, world");

or

static class Test
{
  public static void Main()
  {
    System.Console.WriteLine("hello, world");
  }
}

Test.Main();
Jannchie
  • 690
  • 6
  • 18