If anyone has a better title, let me know.
I made a DisposeHelper so instead of this:
private Something _thing;
void Dispose()
{
if(_thing != null)
{
_thing.Dispose();
_thing = null;
}
}
… i could do this:
private Something _thing;
void Dispose()
{
DiposeHelper.Dipose(ref _thing);
}
But apparently I can’t feed DisposeHelper.Dispose an IDisposable as a reference, unless I cast Something as IDisposable, like so:
private Something _thing;
void Dispose()
{
IDisposable d = _thing;
DiposeHelper.Dipose(ref d);
}
…which would mean it doesn’t then nullify the original field.
Here’s a more abstract example. DoThis works, DoThat doesn’t:
public class Test
{
public Test()
{
Something o = new Something();
DoThis(o);
DoThat(ref o);
}
private void DoThis(IFoo obj) { }
private void DoThat(ref IFoo obj) { }
}
public class Something : IFoo { }
public interface IFoo { }
Why can’t I do it?
I don’t know the technical reason as to why you can’t.
This works, however: