I have this class:
public abstract class ImplementsIDisposable : IDisposable
{
public abstract void Dispose();
private class InnerClass : ImplementsIDisposable
{
private bool disposedValue;
public override void Dispose()
{
if (!disposedValue)
{
doSomething();
disposedValue = true;
}
GC.SuppressFinalize(this);
}
}
}
And Code Analysis is throwing this message:
CA1063 Modify Dispose() so that it calls Dispose(true), then calls GC.SuppressFinalize on the current object instance and then returns.
And this one:
CA1063 Ensure that Dispose() is declared as public and sealed.
Both in the this line:
public abstract void Dispose();
Can it be that it wants Dispose()
to be implemented in ImplementsIDisposable
instead of InnerClass
?
There is no reason why public Dispose()
should be virtual and much less abstract.
You need to check out the dispose pattern and implement it correctly. The warnings are hinting how you should do it but they can be rather cryptic unless you know how the pattern goes to begin with.
You can read about the Dispose Pattern here, it’s rather simple.
And of course, don’t miss this canonical SO answer on the subject.