tags:

views:

331

answers:

6

in vb.net it is end

what is the best way to end a program in c#?

+16  A: 
Application.Exit();

This is the nice way to ensure all Forms are closed and the application shuts down in a controlled way. From MSDN:

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

If this is a console app, use:

Environment.Exit(0);
driis
All good for a WinForms app. You need `Environment.Exit(int)` for a console app/service.
leppie
@leppie, my psychic powers told me it was WinForms ;-) But I added Enviroment.Exit for completeness.
driis
+2  A: 

The best way to exit a program in any language is to naturally hit the end of the programs execution.

In other words. The best way to exit a C# program is to exit the Main method naturally. This will ensure that all resources get cleaned up properly, and it is much easier to follow a program that does exit in the middle of the code.

jjnguy
+24  A: 

It depends on the type of application.

Reed Copsey
+1 for concise.
Platinum Azure
+15  A: 
}

This is what a C# program usually ends with. :)

JavaGeek
this one made my day :)
Maciek
+1 just for the sake of it. You're right, since she didn't say whether a program is a block of code or a running process...
Robert Koritnik
In fairness, some could end with extraneous whitespace afterwards. Or a `#endregion` or other preprocessor directive. Come on. :-P
Platinum Azure
Good point. LOL ;)
JavaGeek
Not always, it can end with a `;` too.
leppie
+2  A: 

I'll give it a shot:

}

Seriously, most well written programs end naturally when the main function ends. You achieve this with careful flow control like break.

Blindy
+1  A: 
try
{

 int i = 1/0;


}

catch (Exception e)

{

     Console.WriteLine("Press any key to continue ..."); 

}


finally 
{
     Console.ReadLine();

}
mumtaz
@downvoter, please do not downvote this
i am a girl
thanks for the kind support :) ... so nice of you !!
mumtaz