End Google Ads 201810 - BS.net 01 --> Hi all,

I need some advice here, I hope somebody can help me.

I have the following class structure (simplified):

public class Bar: IDisposable {...} public abstract class FooBase: IDisposable{ Bar bar; bool disposed; internal FooBase(Bar bar) { this.bar=bar; } public void Dispose() { if (!this.disposed) { this.bar.Dispose(true); GC.SupressFinalize(this); this.disposed = true; } } protected void Dipose(bool disposing) { if (disposing) { this.bar.Dispose(); } }} public FooA: Foo {...}public FooB: Foo {...} public static class FooProvider{ public static FooA GetFooA() { Bar bar = new Bar(); ... return new FooA(bar); } public static FooB GetFooB() { Bar bar = new Bar(); ... return new FooB(bar); } ...}
When I run Code Analysis on this, I get Warnings CA2000 on all 'CreateFooX()' methods of the FooProvider class. This warning gives the following message: "Microsoft. Reliability: In method 'FooProvider.GetFooX()', call System.IDisposable.Dispose on object 'bar' before all references to it are out of scope."

Microsoft recommends to never supress this warning but I'm not really sure its warning about a real problem in the code. True that 'bar' is not disposed before going out of scope in whatever 'CreateFooX()' method we consider but a reference to it lives on in the 'FooX' object which eventually will get disposed and will in turn take care of disposing 'bar'.

Am I understanding something wrong about how the Dispose pattern should work and I have some fundamental flaw in my code or should I just supress this warning?

Thanks for any advice.