简体   繁体   English

C# Console.ReadLine() 从不读取 Linux 中 VSC 上的输入

[英]C# Console.ReadLine() never reads input on VSC in Linux

I've got a Console.ReadLine() inside a finite for loop that never ends reading.我在一个永远不会结束读取的有限for loop中有一个Console.ReadLine()
I am using VS Code on Linux Mint.我在 Linux Mint 上使用 VS Code。 I execute by pressing F5.我按 F5 执行。

using System;

class Person
{
    public string Name { get; set;}
    public override string ToString()
    {
        return "My name is " + Name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int n = 3;
        Person[] p = new Person[n];

        for (int i = 0; i < n; i++)
        {
            p[i] = new Person()
            {
                Name = Console.ReadLine()
            };
            Console.WriteLine("I just read " + p[i]);
        }

        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(p[i].ToString());
        }
    }
}

I expected to input three names and then output them.我希望输入三个名称,然后输入 output 它们。
I input by typing a name and then pressing Enter.我通过输入名称然后按 Enter 来输入。
The issue is that I can keep inputting forever and that Console.WriteLine("I just read " + p[i]);问题是我可以一直输入并且Console.WriteLine("I just read " + p[i]); never gets executed.永远不会被执行。 This happens in the Debug Console.这发生在调试控制台中。

I can't reproduce this on Windows or Ubuntu.我无法在 Windows 或 Ubuntu 上重现此问题。 My guess is that Mint buffers the output.我的猜测是 Mint 缓冲了 output。

Try adding Console.Out.Flush();尝试添加Console.Out.Flush(); after Console.WriteLine("I just read " + p[i]);Console.WriteLine("I just read " + p[i]);

This is still an issue on Linux.这仍然是 Linux 上的问题。 Running Visual Studio Code, in debug mode.在调试模式下运行 Visual Studio Code。 Output is being directed to Debug Console, but input is not being considered. Output 被定向到调试控制台,但未考虑输入。

It only works when running from command line, but that basically disallows for debugging.它仅在从命令行运行时才有效,但这基本上不允许调试。

Though the answer for this problem is here Debug Console window cannot accept Console.ReadLine() input during debugging虽然这个问题的答案在这里Debug Console window cannot accept Console.ReadLine() input during debugging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM