Total .NET Analyzer Rule Documentation  

End Rule

Avoid using End statements.

Remarks

Visual Basic supports the End statement, which stops code execution abruptly. Using the End statement, however, may cause the following serious problems in your code:

1. Any code that you have in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. This means that your objects may not be correctly closed or terminated.

2. Objects created at run time are destroyed and, files are closed, and object references are invalidated.

Because of these risks, you avoid using the End statement in code.

Resolution

Rather than exiting your program using the End statement, you should always unload all forms. This will ensure that objects are closed properly.

Classes often control resources that are not released when the class is finalized. In order to release the most resources, you may wish to call the Dispose method, which finalizes the object and removes the associated COM+ reference. When you are finished using an object, call the Dispose method to release resources and perform tasks such as closing files and database connections. Unlike the Finalize destructor, the Dispose method must be called explicitly.

See Also

Object Lifetime