Исходный код вики Dispose
Версия 1.2 от Alexandr Fokin на 2023/02/15 23:07
Последние авторы
| 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 | _bar = new Bar(); | ||
| 44 | } | ||
| 45 | |||
| 46 | public void Dispose() | ||
| 47 | { | ||
| 48 | _disposable1.Dispose(); | ||
| 49 | _disposable2.Dispose(); | ||
| 50 | } | ||
| 51 | }{{/code}} | ||
| 52 | |(% style="width:465px" %)Наследование. Отчистка неуправляемых и управляемых ресурсов.|(% style="width:1030px" %){{code language="C#"}}class DerivedClassWithFinalizer : BaseClassWithFinalizer | ||
| 53 | { | ||
| 54 | // To detect redundant calls | ||
| 55 | private bool _disposedValue; | ||
| 56 | |||
| 57 | ~DerivedClassWithFinalizer() => this.Dispose(false); | ||
| 58 | |||
| 59 | // Protected implementation of Dispose pattern. | ||
| 60 | protected override void Dispose(bool disposing) | ||
| 61 | { | ||
| 62 | if (!_disposedValue) | ||
| 63 | { | ||
| 64 | if (disposing) | ||
| 65 | { | ||
| 66 | // TODO: dispose managed state (managed objects). | ||
| 67 | } | ||
| 68 | |||
| 69 | // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. | ||
| 70 | // TODO: set large fields to null. | ||
| 71 | _disposedValue = true; | ||
| 72 | } | ||
| 73 | |||
| 74 | // Call the base class implementation. | ||
| 75 | base.Dispose(disposing); | ||
| 76 | } | ||
| 77 | }{{/code}} | ||
| 78 | |||
| 79 |