Если у кого-то есть лучший заголовок, дайте мне знать.
Я сделал DisposeHelper, поэтому вместо этого:
private Something _thing;
void Dispose()
{
if(_thing != null)
{
_thing.Dispose();
_thing = null;
}
}
... я мог бы сделать это:
private Something _thing;
void Dispose()
{
DiposeHelper.Dipose(ref _thing);
}
Но, очевидно, я не могу кормить DisposeHelper. Укажите IDisposable как ссылку, если я не приведу Something как IDisposable, например так:
private Something _thing;
void Dispose()
{
IDisposable d = _thing;
DiposeHelper.Dipose(ref d);
}
... что означает, что это незатем обнулите исходное поле.
Вот более абстрактный пример.DoThis работает, DoThat не:
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 { }
Почему я не могу это сделать?