<p>Having read the threads <a href="https://stackoverflow.com/questions/60919/is-sqlcommanddispose-enough">Is SqlCommand.Dispose enough?</a> and <a href="https://stackoverflow.com/questions/23867/closing-and-disposing-a-wcf-service">Closing and Disposing a WCF Service</a> I am wondering for classes such as SqlConnection or one of the several classes inheriting from the Stream class does it matter if I close Dispose rather than Close?</p>

[{'answer_id': 61096, 'author': 'Curt Hagenlocher', 'author_id': 533, 'author_profile': 'https://Stackoverflow.com/users/533', 'pm_score': 4, 'selected': False, 'text': '<p>For <code>SqlConnection</code>, from the perspective of the connection itself, they are equivalent.  According to Reflector, <code>Dispose()</code> calls <code>Close()</code> as well as doing a few additional memory-freeing operations -- mostly by setting members equal to null.</p>\n\n<p>For Stream, they actually are equivalent.  <code>Stream.Dispose()</code> simply calls Close().</p>\n'}, {'answer_id': 61107, 'author': 'Tyler', 'author_id': 5642, 'author_profile': 'https://Stackoverflow.com/users/5642', 'pm_score': 4, 'selected': False, 'text': '<p><strong>You DO need to call Dispose()!</strong></p>\n\n<p>Dispose() is for the developer to call, the Garbage Collector calls Finalize(). If you don\'t call Dispose() on your objects any unmanaged resources that they used won\'t be disposed until the garbage collector comes around and calls finalize on them (and who knows when that will happen).</p>\n\n<p>This scenario is called Non Deterministic Finalization and is a common trap for .net developers. If you\'re working with objects that implement IDisposable then call Dispose() on them!</p>\n\n<p><a href="http://www.ondotnet.com/pub/a/oreilly/dotnet/news/programmingCsharp_0801.html?page=last" rel="noreferrer">http://www.ondotnet.com/pub/a/oreilly/dotnet/news/programmingCsharp_0801.html?page=last</a></p>\n\n<p>While there may be many instances (like on SqlConnection) where you call Disponse() on some object and it simply calls Close() on it\'s connection or closes a file handle, <strong>it\'s almost always your best bet to call Dispose()! unless you plan on reusing the object in the very near future.</strong></p>\n'}, {'answer_id': 61131, 'author': 'Brannon', 'author_id': 5745, 'author_profile': 'https://Stackoverflow.com/users/5745', 'pm_score': 5, 'selected': False, 'text': "<p>As usual the answer is: it depends.  Different classes implement <code>IDisposable</code> in different ways, and it's up to you to do the necessary research.</p>\n\n<p>As far as <code>SqlClient</code> goes, the recommended practice is to do the following:</p>\n\n<pre><code>using (SqlConnection conn = /* Create new instance using your favorite method */)\n{\n    conn.Open();\n    using (SqlCommand command = /* Create new instance using your favorite method */)\n    {\n        // Do work\n    }\n    conn.Close(); // Optional\n}\n</code></pre>\n\n<p>You <strong>should</strong> be calling <code>Dispose</code> (or <code>Close</code>*) on the connection!  Do <strong>not</strong> wait for the garbage collector to clean up your connection, this will tie up connections in the pool until the next GC cycle (at least).  If you call <code>Dispose</code>, it is not necessary to call <code>Close</code>, and since the <code>using</code> construct makes it so easy to handle <code>Dispose</code> correctly, there is really no reason to call <code>Close</code>.</p>\n\n<p>Connections are automatically pooled, and calling <code>Dispose</code>/<code>Close</code> on the connection does not physically close the connection (under normal circumstances).  Do not attempt to implement your own pooling.  <code>SqlClient</code> performs cleanup on the connection when it's retrieved from the pool (like restoring the database context and connection options).</p>\n\n<p>*if you are calling <code>Close</code>, make sure to do it in an exception-safe way (i.e. in a catch or finally block).</p>\n"}, {'answer_id': 61133, 'author': 'André Chalella', 'author_id': 4850, 'author_profile': 'https://Stackoverflow.com/users/4850', 'pm_score': 3, 'selected': False, 'text': '<p><em>This would-be quick advice became a long answer. Sorry.</em></p>\n\n<p>As tyler pointed out in his nice answer, calling <code>Dispose()</code> is a great programming practice. This is because this method is supposed to "rally together" all the resource-freeing needed so there are no unneeded open resources. If you wrote some text to a file, for example, and failed to close the file (free the resource), it will remain open and no one else will be able to write to it until the GC comes around and does what you should have done.</p>\n\n<p>Now, in some cases there will be "finalizing" methods more specific to the class you\'re dealing with, like <code>StreamWriter.Close()</code>, which overrides <code>TextWriter.Close()</code>. Indeed they are usually more suited to the situation: a StreamWriter\'s <code>Close()</code>, for example, flushes the stream and the underlying encoder before <code>Dispose()</code>ing of the object! Cool!</p>\n\n<p>However, browsing MSDN you\'ll find that even Microsoft is sometimes confused by the multitude of closers and disposers. <a href="http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx" rel="noreferrer" title="How to: Write Text to a File">In this webpage</a>, for instance, in some examples <code>Close()</code> is called before the implicit <code>Dispose()</code> (see <a href="http://msdn.microsoft.com/en-us/library/yh598w02.aspx" rel="noreferrer" title="using Statement (C# Reference)">using statement</a> if you don\'t understand why it\'s implicit), and in one in particular they don\'t bother to. Why would that be? I too was perplexed.</p>\n\n<p>The reason I figured (and, I stress, this is <a href="http://en.wikipedia.org/wiki/Original_research" rel="noreferrer" title="Original research - Wikipedia, the free encyclopedia">original research</a> and I surely might lose reputation if I\'m wrong) is that <strong><code>Close()</code> might fail, yielding an exception whilst leaving resources open, while <code>Dispose()</code> would surely free them</strong>. Which is why <strong>a <code>Dispose()</code> should always safeguard a <code>Close()</code> call</strong> (sorry for the pun).</p>\n\n<pre><code>MyResource r = new MyResource();\n\ntry {\n  r.Write(new Whatever());\n\n  r.Close()\nfinally {\n  r.Dispose();\n}\n</code></pre>\n\n<p>And yes, I guess Microsoft slipped on that one example. Perhaps that timestamp would never get flushed to the file.</p>\n\n<p>I\'m fixing my old code tomorrow.</p>\n\n<p><em>Edit: sorry Brannon, I can\'t comment on your answer, but are you sure it\'s a good idea to call <code>Close()</code> on a <code>finally</code> block? I guess an exception from that might ruin the rest of the block, which likely would contain important cleanup code.</em></p>\n\n<p><em>Reply to Brannon\'s: great, just don\'t forget to call <code>Close()</code> when it is really needed (e.g. when dealing with streams - don\'t know much about SQL connections in .NET).</em></p>\n'}, {'answer_id': 61171, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 9, 'selected': True, 'text': '<h3>I want to clarify this situation.</h3>\n<p>According to Microsoft guidelines, it\'s a good practice to provide <code>Close</code> method where suitable. <a href="https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern" rel="noreferrer">Here</a> is a citation from <a href="https://rads.stackoverflow.com/amzn/click/com/0321246756" rel="noreferrer" rel="nofollow noreferrer">Framework design guidelines</a></p>\n<blockquote>\n<p>Consider providing method <code>Close()</code>, in addition to the <code>Dispose()</code>, if close is standard terminology in the area. When doing so, it is important that you make the <code>Close</code> implementation identical to <code>Dispose</code> ...</p>\n</blockquote>\n<p>In most of cases <code>Close</code> and <code>Dispose</code> methods are equivalent. The <strong>main difference</strong> between <code>Close</code> and <code>Dispose</code> in the case of <code>SqlConnectionObject</code> is:</p>\n<blockquote>\n<p>An application can call <code>Close</code> more\nthan one time. No exception is\ngenerated.</p>\n<p>If you called <code>Dispose</code> method\n<code>SqlConnection</code> object state will be\nreset. If you try to call any\nmethod on disposed <code>SqlConnection</code>\nobject, you will receive exception.</p>\n</blockquote>\n<p>That said:</p>\n<ul>\n<li><strong>If you use connection object one\ntime, use <code>Dispose</code>. A <code>using</code> block will ensure this is called even in the event of an exception.</strong></li>\n<li><strong>If connection object must be reused,\nuse <code>Close</code> method.</strong></li>\n</ul>\n'}, {'answer_id': 3217006, 'author': 'supercat', 'author_id': 363751, 'author_profile': 'https://Stackoverflow.com/users/363751', 'pm_score': 2, 'selected': False, 'text': '<p>Typecast to iDisposable, and call dispose on that.  That will invoke whatever method is configured as implementing "iDisposable.Dispose", regardless of what the function is named.</p>\n'}, {'answer_id': 44633468, 'author': 'Deep Jadia', 'author_id': 7437225, 'author_profile': 'https://Stackoverflow.com/users/7437225', 'pm_score': 1, 'selected': False, 'text': "<p>Generally we are facing issue in Close(), Abort() and Dispose() but let me tell you difference among them.</p>\n\n<p>1) ABORT:- I won't suggest to use this because when abort is called the client will delete the connection without telling the server so server will wait for some amount of time (approximately 1 min). If you having bulk request then you can't use abort() because it may caused time out for your limited connection pool.</p>\n\n<p>2) Close:- Close is very good way to closing the connection because when closing the connection it will call server and acknowledge the server to close by that side too.</p>\n\n<p>Here, one more thing to look.\nIn some cases, if error generates then it is not a good way to write code in finally that connection.close() because at that time Communication State will be faulted. </p>\n\n<p>3) Dispose :- It is one type of close but after closing the connection you can not open it again. </p>\n\n<p>So try this way ,</p>\n\n<pre><code>private void CloseConnection(Client client)\n    {\n        if (client != null &amp;&amp; client.State == CommunicationState.Opened)\n        {\n            client.Close();\n        }\n        else\n        {\n            client.Abort();\n        }\n    }\n</code></pre>\n"}, {'answer_id': 73442564, 'author': 'Alexander Kozachenko', 'author_id': 3423333, 'author_profile': 'https://Stackoverflow.com/users/3423333', 'pm_score': 0, 'selected': False, 'text': "<p>I just had an issue on net6 app using <code>TransactionScope</code>: I had multiple consequently created, opened and disposed connections, but was catching <code>This platform does not support distributed transactions</code> on completing scope (while clearly there were no distributed transactions).</p>\n<p>The issue was resolved by adding <code>Close</code> method call. The majority of answers say that <code>Dispose</code> and <code>Close</code> are identical, but it seems they aren't: from what I gleaned from source code, <code>Close</code> method explicitly returns connection to pool, while I could not find identical code for <code>Dispose</code>.</p>\n"}]
['https://Stackoverflow.com/questions/61092', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5086/']