In the following code, I'm implementing an interface, and then deriving from that class and implementing the same interface. When I dispose an instance of the derived class, it only calls the derived implementation.
Why wouldn't the C# compiler warn about this? It seems dangerous for a couple reasons. I could implement an interface that the base had implemented, which would give me behavior I wasn't expecting (such as the base no longer disposing). Or if I later decide to have a base implement a new interface, I have to scan all my code to find any derivatives that could be implementing it already.
For duplicate members in derived classes, the compiler requires us to use 'override' or 'new'.
Why are interfaces different?
class CA : IDisposable
{
void IDisposable.Dispose() { Debug.WriteLine("CA.Dispose"); }
}
class CB : CA, IDisposable
{
void IDisposable.Dispose() { Debug.WriteLine("CB.Dispose"); }
}
class Program
{
static void Main(string[] args)
{
using (new CB()) { }
}
}
// output: CB.Dispose