Исходный код вики Dispose
Версия 1.4 от Alexandr Fokin на 2023/02/15 23:09
Последние авторы
| author | version | line-number | content |
|---|---|---|---|
| 1 | Реализация метода Dispose | ||
| 2 | [[https:~~/~~/learn.microsoft.com/ru-ru/dotnet/standard/garbage-collection/implementing-dispose>>https://learn.microsoft.com/ru-ru/dotnet/standard/garbage-collection/implementing-dispose]] | ||
| 3 | |||
| 4 | ---- | ||
| 5 | |||
| 6 | |(% style="width:465px" %)Отчистка неуправляемых и управляемых ресурсов|(% style="width:1030px" %){{code language="C#"}}class BaseClassWithFinalizer : IDisposable | ||
| 7 | { | ||
| 8 | // To detect redundant calls | ||
| 9 | private bool _disposedValue; | ||
| 10 | |||
| 11 | ~BaseClassWithFinalizer() => Dispose(false); | ||
| 12 | |||
| 13 | // Public implementation of Dispose pattern callable by consumers. | ||
| 14 | public void Dispose() | ||
| 15 | { | ||
| 16 | Dispose(true); | ||
| 17 | GC.SuppressFinalize(this); | ||
| 18 | } | ||
| 19 | |||
| 20 | // Protected implementation of Dispose pattern. | ||
| 21 | protected virtual void Dispose(bool disposing) | ||
| 22 | { | ||
| 23 | if (!_disposedValue) | ||
| 24 | { | ||
| 25 | if (disposing) | ||
| 26 | { | ||
| 27 | // TODO: dispose managed state (managed objects) | ||
| 28 | } | ||
| 29 | |||
| 30 | // TODO: free unmanaged resources (unmanaged objects) and override finalizer | ||
| 31 | // TODO: set large fields to null | ||
| 32 | _disposedValue = true; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | }{{/code}} | ||
| 36 | |(% style="width:465px" %)Отчистка управляемых ресурсов|(% style="width:1030px" %){{code language="C#"}}public sealed class Foo : IDisposable | ||
| 37 | { | ||
| 38 | private readonly IDisposable _disposable1; | ||
| 39 | private readonly IDisposable _disposable2; | ||
| 40 | |||
| 41 | public Foo() | ||
| 42 | { | ||
| 43 | _disposable1 = new Bar(); | ||
| 44 | _disposable2 = new Bar(); | ||
| 45 | } | ||
| 46 | |||
| 47 | public void Dispose() | ||
| 48 | { | ||
| 49 | _disposable1.Dispose(); | ||
| 50 | _disposable2.Dispose(); | ||
| 51 | } | ||
| 52 | }{{/code}} | ||
| 53 | |(% style="width:465px" %)Наследование. Отчистка неуправляемых и управляемых ресурсов.|(% style="width:1030px" %){{code language="C#"}}class DerivedClassWithFinalizer : BaseClassWithFinalizer | ||
| 54 | { | ||
| 55 | // To detect redundant calls | ||
| 56 | private bool _disposedValue; | ||
| 57 | |||
| 58 | ~DerivedClassWithFinalizer() => this.Dispose(false); | ||
| 59 | |||
| 60 | // Protected implementation of Dispose pattern. | ||
| 61 | protected override void Dispose(bool disposing) | ||
| 62 | { | ||
| 63 | if (!_disposedValue) | ||
| 64 | { | ||
| 65 | if (disposing) | ||
| 66 | { | ||
| 67 | // TODO: dispose managed state (managed objects). | ||
| 68 | } | ||
| 69 | |||
| 70 | // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. | ||
| 71 | // TODO: set large fields to null. | ||
| 72 | _disposedValue = true; | ||
| 73 | } | ||
| 74 | |||
| 75 | // Call the base class implementation. | ||
| 76 | base.Dispose(disposing); | ||
| 77 | } | ||
| 78 | }{{/code}} |