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

前端 未结 21 2278
死守一世寂寞
死守一世寂寞 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 03:56

    I assume the reason you don't want it to close in Debug mode, is because you want to look at the values of variables etc. So it's probably best to just insert a break-point on the closing "}" of the main function. If you don't need to debug, then Ctrl-F5 is the best option.

    0 讨论(0)
  • 2020-11-22 03:57

    I always add the following statement to a console application.(Create a code snippet for this if you wish)

    Console.WriteLine("Press any key to quit!");
    Console.ReadKey();
    

    Doing this helps when you want to experiment different concepts through console application.

    Ctr + F5 will make the Console stay but you cant debug! All the console applications that I have written in realworld is always non-interactive and triggered by a Scheduler such as TWS or CA Work station and did not require something like this.

    0 讨论(0)
  • 2020-11-22 03:57

    To simplify what others are saying: Use Console.ReadKey();.

    This makes it so the program is waiting on the user to press a normal key on the keyboard

    Source: I use it in my programs for console applications.

    0 讨论(0)
  • 2020-11-22 03:58

    Add The Read method to show the output.

    Console.WriteLine("Hello, World!");
    Console.Read();
    return 0;
    
    0 讨论(0)
  • 2020-11-22 03:59

    Alternatively, you can delay the closing using the following code:

    System.Threading.Thread.Sleep(1000);
    

    Note the Sleep is using milliseconds.

    0 讨论(0)
  • 2020-11-22 03:59

    You can solve it very simple way just invoking the input. However, if you press Enter then the console will disapper again. Simply use this Console.ReadLine(); or Console.Read();

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