简体   繁体   中英

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 method. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM