'Why exiting "using" invokes unexpected Dispose()?
Here is my case:
class A : IDisposable
{
public void Dispose() { Console.WriteLine("A Dispose"); }
}
class B : A
{
public new void Dispose(){ Console.WriteLine("B Dispose"); }
}
class Program
{
static void Main(string[] args)
{
using (B b = new B())
{
}
}
}
Final output is "A Dispose". I've read some documents which introduce the difference of override and new, and something telling that "using" substantially equals to "try-finally". But I am still can't get answer to why it is not B::Dispose got automatically invoked. Comparingly following's output is "B Dispose"
B b = new B();
try
{
}
finally
{
b.Dispose();
}
Please help, which did I miss.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
