Click here to Skip to main content
15,612,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
For example, in C #, I want the automatic program to be closed in the application console environment with the correctness of the condition, and if the condition is not met, the program will continue until the end of the program is closed by pressing the key
int i = int.Parse(Console.ReadLine());
Console.WriteLine("num not ok ");
//I want to close the program here

What I have tried:

I did all the possible ways to do this but unfortunately I could not get the result and tried different methods
Updated 14-Jan-22 2:55am

It really depends on where that code is: if it's part of your Main method, then teh simplest solution is just
if(i > 10)
   Console.WriteLine("num not ok ");
   return -1;
The return statement exits the current method immediately, and in the case of the Main method, that also ends the program - the integer that you return is passed to the system as a "reason for exit" where zero is "normal exit" and any nonm-zero value is an error code.

If you aren't in the Main function, then you need to use
instead of return
Share this answer
Use the following code.
0 is Exit code, Read more related to ExitCode
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900