8
I confess that I still don’t understand everything about how to manipulate the destruction of an object in the C# and now, as I’m testing Visual Studio 2015 Preview, when implementing a database connection class I went to include the inheritance of IDisposable
and I used the option Implement interface with Dispose Pattern.
Then the following code was included in my class:
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a
// finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code
// to free unmanaged resources.
// ~DBConnection() {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
I couldn’t understand these comments with All on the method finalize ~DBConnection()
, about the GC.SuppressFinalize(this);
and on the method Dispose(bool disposing)
and all its contents...
Checking further options saw the following implementation:
public void Dispose()
{
((IDisposable)connection).Dispose();
}
And of course, we have the following:
public void Dispose()
{
throw new NotImplementedException();
}
That I would implement as is usually shown in examples on the web:
public void Dispose()
{
if (connection != null && connection.State == ConnectionState.Open)
{
connection.Close();
connection = null;
}
GC.SuppressFinalize(this);
}
Well, on the first example of implementation with all those comments, I understood that there is an explanation of the implementation options.
Can anyone help me understand the difference between these methods and the indications of the first method with the comments?
You just need to implement
IDisposable
if using resources not managed by CLR. https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110). aspx– bfavaretto
but @bfavaretto, what would be the "unmanaged resources"? And what is the difference between the ways of implementing
IDisposable
? Finally, after I’ll create a question about managing connections in c#. Pool of connections, more than onecommand
andreader
... let’s see.. Thank you!– JamesTK
James, everything you program on . net is compiled into an intermediate language interpreted by the . net virtual machine (the CLR). This type of code is managed by CLR (which handles garbage collection, among other bureaucracies). Code compiled for machine language (for example, written in C) can be integrated into your program, but is not managed by CLR and you need to manually take care of certain details. But I’ll stop here and let . net experts answer :)
– bfavaretto