qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbffb00000000000a500000001000900
I see things surface from time to time on the question of Dispose vs. Close on objects that implement IDisposable. I posted a while back my ideas on why calling Dispose is not only correct, but necessary. To me the facts are clear, but you'll find the question arise frequently. There seems to be two areas of FUD (fear, uncertainty, doubt) surrounding the use of Dispose.
0) Not knowing what Dispose is, or why to use it. Why is it that any .NET book with a section on data access completely omits any explanation of Dispose? In fact, any code samples you'll find in the book (even samples you'll see in the Framework QuickStarts or on MSDN) will simply call Close and never Dispose? I have no idea why. By definition alone, the purpose of the Dispose method of classes that implement IDisposable is to perform application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. Not sure why that would be something you wouldn't want to happen immediately. When I am done with something that holds references to unmanaged resources, I want those resources released.
1) In the case of connections, fear that calling Dispose on a connection removes the connection from the pool. This is not correct. The connection pooler will remove a connection from the pool if the connection lifetime has expired, or if the pooler detects that the connection with the server has been severed. Calling Dispose does not produce this effect. Calling Close on the connection will return it to the pool. However, calling Dispose both returns it to the pool and releases unmanaged resources from the active process. I think it is the statement that “unmanaged resources are released” is where the confusion comes from about Dispose not returning connections to the pool. You can read more on connection pooling in the MSDN article: Connection Pooling for the .NET Framework Data Provider for SQL Server.