Why the 2nd nested 'using' ( with my Foo, Bar classes) not generate the CA2202 "Do not dispose objects multiple times" warning. Same nested "using" for the IO types do generate the CA2202 issue.
Thank you all - any help will be appreciated.
using System;
using System.IO;
namespace ConsoleApplication1
{
public class Foo : IDisposable
{
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isUserCall)
{
if (isUserCall)
{
}
else
{
}
}
}
public class Bar : IDisposable
{
private Foo _foo;
public Bar(Foo foo)
{
_foo = foo;
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isUserCall)
{
if (isUserCall)
{
// call Foo::Dispose as StreamWriter dispose the FileStream
_foo.Dispose();
}
}
}
class Program
{
static void Main(string[] args)
{
// generate FxCop CA2202 with .NET types
using (Stream stream = new FileStream("file.txt", FileMode.OpenOrCreate))
{
using (StreamWriter writer = new StreamWriter(stream))
{
// Use the writer object...
}
}
// not generate the issue with my types... why ?
using (Foo f = new Foo())
{
using (Bar b = new Bar(f))
{
}
}
}
}
}