Visual Studio Code Cant get output from a C# code

流过昼夜 提交于 2019-12-24 07:13:05

问题


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.


回答1:


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

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



回答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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/45003803/visual-studio-code-cant-get-output-from-a-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!