0

After I run a program, I always need to add Console.ReadLine(); to the end in order for the program results to not go away. If I were to code:

static void Main(string[] args) {
  Console.WriteLine("Hello World!");
  Console.ReadLine(); // I need this for the console to stay open.
}

I noticed in watching youtube videos that some are able to run their code without the Console.ReadLine() method. How do I go about doing this? Thanks!

States
  • 43
  • 7

2 Answers2

0

From Visual Studio, you can press Ctrl+F5 to run your program, that will run without debugging. You can also use Console.ReadKey() as an alternative to Console.ReadLine() for keeping the console visible.

Chandan Rai
  • 9,879
  • 2
  • 20
  • 28
0

You can also run another thread. Console stay while it lasts. EDIT: I just put the sample code here for better readability:

var thread = new System.Threading.Thread(
    () => {while (true) System.Threading.Thread.Sleep(5000);}
);
thread.Start();
Bartek
  • 19
  • 3
  • i know it's old and you probably found an answer, but for others looking for it: `var thread = new System.Threading.Thread(() => { while (true) System.Threading.Thread.Sleep(5000); }); thread.Start();` It holds console (because program is still running) until you close it manually. – Bartek Nov 20 '19 at 15:02