Game Development Reference
In-Depth Information
if (disposing)
// Perform cleanup on managed objects
// Perform cleanup on unmanaged objects
Certain cases, like a database connection or network socket, are better suited to a
Close method instead of Dispose . The best way to handle this situation is to have a
Dispose , finalizer, and a Close method that all point to the same function. In most
cases, the Dispose method will be privately declared.
The general rule of thumb is to implement IDisposable and provide a Dispose
method if the class has a finalizer. Also, in situations where you know you are done
with an object, you should call the Dispose method explicitly instead of waiting for
the finalizer to fire.
The C# language provides a great mechanism that automatically disposes objects
that implement the IDisposable interface. The using keyword allows you to speci-
fy a block of code that will call the Dispose method when program execution leaves
the construct.
The following code shows how to use the construct:
using (DisposableType disposableObject)
// Use disposableObject for something
// At this point the disposableObject.Dispose() method has been called.
Another great feature about the using keyword is that you can be guaranteed the
Dispose method will fire, even if an exception is thrown from within the construct.
Implement the dispose design pattern on resources that need to be explicitly freed, and always
implement IDisposable if the class provides a finalizer.
Lastly, throw an ObjectDisposedException from methods where the unmanaged resources are
needed but have already been disposed. The only place you should not do this is in the dispose
method itself because it should be callable any number of times without throwing an exception.
Search Nedrilad ::

Custom Search