Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing an IDisposable object by reference causes an error?
    primarykey
    data
    text
    <p>I am trying to create a general method for disposing an object that implements <a href="http://msdn.microsoft.com/en-us/library/system.idisposable(loband).aspx" rel="nofollow noreferrer">IDisposable</a>, called <code>DisposeObject()</code><br /></p> <p>To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference.</p> <p>But I am getting a compilation error that says</p> <blockquote> <p>The 'ref' argument type doesn't match parameter type</p> </blockquote> <p>In the below (simplified) code, both <code>_Baz</code> and <code>_Bar</code> implement <a href="http://msdn.microsoft.com/en-us/library/system.idisposable(loband).aspx" rel="nofollow noreferrer">IDisposable</a>.<br /></p> <p><img src="https://farm4.static.flickr.com/3576/3479637489_d92cf9f406_o.png" alt="alt text"></p> <p>So the questions are,<br /></p> <ol> <li>Why am I getting this error?</li> <li>Is there a way to get around it?</li> </ol> <p><strong>[UPDATE]</strong> From provided answers so far, as long as I do not set an IDisposable argument to null, I can simply pass an object by value without using <code>ref</code>. I am now having another trouble whether to set disposable objects to <code>null</code> or not within <code>DisposeObject</code> method. <br /></p> <p>Here is the full source for completeness:</p> <pre><code>public class Foo : IDisposable { private Bar _Bar; private Baz _Baz; private bool _IsDisposed; ~Foo() { Dispose(false); } public void Dispose(bool disposing) { if (!_IsDisposed) { if (disposing) { DisposeObject(ref _Baz); DisposeObject(ref _Bar); } } _IsDisposed = true; } private void DisposeObject(ref IDisposable obj) { try { if (obj == null) return; obj.Dispose(); obj = null; } catch (ObjectDisposedException) { /* Already Disposed... */ } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } } public class Bar : IDisposable { public void Dispose() {} } public class Baz : IDisposable { public void Dispose() {} } </code></pre> <p><strong>[RESULT]</strong> <br /> I removed the code that sets argument to null (<code>obj = null;</code>) within <code>DisposeObject</code> So the final code became.</p> <pre><code> public void Dispose(bool disposing) { if (!_IsDisposed) { if (disposing) { DisposeObject(_Baz); DisposeObject(_Bar); } } _IsDisposed = true; } private void DisposeObject(IDisposable obj) { try { if (obj == null) return; obj.Dispose(); } catch (ObjectDisposedException) { /* Already Disposed... */ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload