Why is the console window closing immediately once displayed my output?

前端 未结 21 2277
死守一世寂寞
死守一世寂寞 2020-11-22 03:45

I\'m studying C# by following the guides in MSDN.

Now, I just tried the Example 1 (here is the link to MSDN), and I\'ve encountered an i

相关标签:
21条回答
  • 2020-11-22 04:12

    This behaves the same for CtrlF5 or F5. Place immediately before end of Main method.

    using System.Diagnostics;
    
    private static void Main(string[] args) {
    
      DoWork();
    
      if (Debugger.IsAttached) {
        Console.WriteLine("Press any key to continue . . .");
        Console.ReadLine();
      }
    }
    
    0 讨论(0)
  • 2020-11-22 04:14

    Use Console.Read(); to prevent the program from closing, but make sure you add the Console.Read(); code before return statement, or else it will be a unreachable code .

        Console.Read(); 
        return 0; 
    

    check this Console.Read

    0 讨论(0)
  • 2020-11-22 04:14

    Add the following before the return 0:

    system("PAUSE");  
    

    This prints a line to hit a key to close the window. It will keep the window up until you hit the enter key. I have my students add it to all their programs.

    0 讨论(0)
提交回复
热议问题