Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.ReadLine() not working in VS Code, writing a code in C#

I am learning C# and I am using VS Code, when I tried to take input from user using Console.ReadLine() it's not working.

I referred from a video which did exactly same thing and still after a couple of Google searches I couldn't figure out where I went wrong.

Console.WriteLine("Enter your name : ");
string name = Console.ReadLine();
Console.WriteLine("Hi! {0}", name);

My main method has only this 3 lines and it always gives as output something like this:

enter image description here

Screencast: enter image description here

It never prints the last line of code

like image 753
Marx Avatar asked Jan 09 '20 12:01

Marx


2 Answers

This was happening because the default console in VS Code was external terminal. If you set it integrated terminal, it allows you to take input from user.

It can be done by setting "console": "integratedTerminal" in launch.json file.

like image 103
Marx Avatar answered Sep 23 '22 23:09

Marx


it actually works, but it happens so fast you can not see it.

try adding Console.ReadKey(); this way the program will not stop until you press a random key on your keyboard.

like image 20
Morris Avatar answered Sep 23 '22 23:09

Morris