Should “Dispose” only be used for types containing unmanaged resources?

后端 未结 15 1617
执笔经年
执笔经年 2020-12-05 01:41

I was having a discussion with a colleague recently about the value of Dispose and types that implement IDisposable.

I think there is value

15条回答
  •  温柔的废话
    2020-12-05 01:54

    In one of my projects I had a class with managed threads inside it, we'll call them thread A, and thread B, and an IDisposable object, we'll call it C.

    A used to dispose of C on exiting. B used to use C to save exceptions.

    My class had to implement IDisposable and a descrtuctor to ensure things are disposed of in the correct order. Yes the GC could clean up my items, but my experience was there was a race condition unless I managed the clean up of my class.

提交回复
热议问题