ShdwKnght333
ShdwKnght333

Reputation: 330

Visual Studio Code Cant get output from a C# code

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.

Upvotes: 1

Views: 5915

Answers (5)

Jannchie
Jannchie

Reputation: 1008

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

Upvotes: 0

Todd Bradley
Todd Bradley

Reputation: 139

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.

Upvotes: 2

Vasil
Vasil

Reputation: 312

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

Upvotes: 0

Alexandra
Alexandra

Reputation: 413

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++?

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

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

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

Upvotes: 2

Related Questions