Reputation: 14548
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
Upvotes: 4
Views: 227
Reputation: 1500395
It's only doing that because you're using explicit interface implementation. For example, this doesn't compile:
using System;
using System.Diagnostics;
class CA : IDisposable
{
public void Dispose()
{
Debug.WriteLine("CA.Dispose");
}
}
class CB : CA, IDisposable
{
public void Dispose()
{
Debug.WriteLine("CB.Dispose");
}
}
So yes, you need to be careful with explicit interface implementation. That's always the case, as it's a little odd in terms of the type system anyway.
It's not ideal, but explicit interface implementation is there partly to get round awkward situations where you want to be able to reimplement an interface.
To be honest, if you decide at a late stage that a base class should start to implement IDisposable
, you're in a world of hurt anyway - because you'll have to inspect every place you're using that class in order to tell whether or not you need a using
statement.
Upvotes: 6