Who Disposes of an IDisposable public property?

前端 未结 10 1655
萌比男神i
萌比男神i 2020-12-03 09:59

If I have a SomeDisposableObject class which implements IDisposable:

class SomeDisposableObject : IDisposable
{
    public void Dis         


        
10条回答
  •  天涯浪人
    2020-12-03 10:38

    You could just flag the Disposal in Dispose(). After all Disposal isn't a destructor - the object still exists.

    so:

    class AContainer : IDisposable
    {
        bool _isDisposed=false;
    
        public void Dispose()
        {
            if (!_isDisposed) 
            {
               // dispose
            }
            _isDisposed=true;
        }
    }
    

    add this to your other class, too.

提交回复
热议问题