Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you wait for input on the same Console.WriteLine() line?

Tags:

c#

People also ask

How do I wait for input in C#?

ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen).

How does WriteLine () method works?

WriteLine(String, Object, Object) Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

How do you go to the next line in console WriteLine?

By using: \n – It prints new line. By using: \x0A or \xA (ASCII literal of \n) – It prints new line. By using: Console. WriteLine() – It prints new line.

What does the console ReadLine () line do?

The ReadLine method reads a line from the standard input stream. (For the definition of a line, see the paragraph after the following list.) This means that: If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key.


Use Console.Write instead, so there's no newline written:

Console.Write("What is your name? ");
var name = Console.ReadLine();

As Matt has said, use Console.Write. I would also recommend explicitly flushing the output, however - I believe WriteLine does this automatically, but I'd seen oddities when just using Console.Write and then waiting. So Matt's code becomes:

Console.Write("What is your name? ");
Console.Out.Flush();
var name = Console.ReadLine();