Home > Enterprise >  What is the difference between Close(); and Application.Exit();?
What is the difference between Close(); and Application.Exit();?

Time:12-03

This isn't really a big problem but I have been using the Close(); statement to well make the app close or have a button that closes the program with this function. However recently I saw someone use Application.Exit(); to do the same I guess. I just wanna know how these two are different. Or are they the same?

I haven't tried the Application.Exit(); statement yet. I just wanna know the difference because I am basically just a beginner.

CodePudding user response:

It has been 10 years that I have not used windows forms but as far as I remember: Close, closes the current form. Just if the current form is the application main form, this will cause the application to exit. Application.Exit() could be called anywhere in any form and it causes application termination.

CodePudding user response:

In C#, the Close() method is used to close the current form or window, while the Application.Exit() method is used to terminate the entire application.

The Close() method is a member of the Form class, and it is used to close the current form or window. This method will close the form or window, and control will be returned to the caller. If the form or window is the main application window, the application will continue to run, and other forms or windows can still be opened.

The Application.Exit() method, on the other hand, is a static method of the Application class, and it is used to terminate the entire application. This method will close all open forms or windows, and will terminate the application. Once the application has been terminated, control will not be returned to the caller, and the application will no longer be running.

Overall, the main difference between Close() and Application.Exit() is that Close() only closes the current form or window, while Application.Exit() terminates the entire application. Depending on your specific requirements, you can use either method to close forms or windows in your C# application.

  • Related