开发者

Is it possible to not implement a method inherited from an interface in C#?

Looking at the Project Server 2010 SDK (found here in .NET Reflector, I have found something interesting that confuses me.

Apparently, the SvcProject.ProjectClient class in ProjectServerServices.dll inherits from System.ServiceModel.ClientBase<Project>, which is an abstract class that implements the System.IDisposable interface. However, when I inspect the SvcProject.ProjectClient class (which is not abstract), there is no Dispose m开发者_运维技巧ethod. What gives? I thought that every method inherited from interfaces had to be implemented in a concrete implementation (otherwise what's the use of interfaces). Is .NET Reflector lying to me? Did Microsoft for some reason circumvent this requirement? What's going on?


It's probably been implemented explicitly, like this:

void IDisposable.Dispose()
{
    // Code here
}

Either that or it inherits the method from the base class. Just because ClientBase<T> is abstract doesn't mean that it can't have implemented IDisposable properly itself. Indeed, it will have to either implemented it or redeclared it as an abstract method to force the derived class to implement it.

Either way, the method will be there somehow.

Explicit interface implementation means that the method is only available when the instance is viewed via the interface type. For example:

class Foo : IDisposable
{
    void IDisposable.Dispose() {}
}

...

Foo foo = new Foo();
foo.Dispose(); // Invalid: compile time error
IDisposable d = foo;
d.Dispose();   // Valid
((IDisposable)foo).Dispose(); // Valid (no need for temp variable)


Without looking, I would say that the base class provides the Dispose method and the concrete class simply doesn't override it. EDIT: And (after looking), it does provide a non-abstract, explicit implementation as IDisposable.Dispose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜