org.sonar.plugins.csharp.S4039.html Maven / Gradle / Ivy
Why is this an issue?
When a base type explicitly implements a public interface method, that method is only accessible in derived types through a reference to the
current instance (namely this
). If the derived type explicitly overrides that interface method, the base implementation becomes
inaccessible.
This rule raises an issue when an unsealed, externally visible type provides an explicit method implementation of a public interface
and does not provide an alternate, externally visible method with the same name.
Noncompliant code example
public interface IMyInterface
{
void MyMethod();
}
public class Foo : IMyInterface
{
void IMyInterface.MyMethod() // Noncompliant
{
MyMethod();
}
void MyMethod()
{
// Do something ...
}
}
public class Bar : Foo, IMyInterface
{
public void MyMethod()
{
// Can't access base.MyMethod()
// ((IMyInterface)this).MyMethod() would be a recursive call
}
}
Compliant solution
public interface IMyInterface
{
void MyMethod();
}
public class Foo : IMyInterface
{
void IMyInterface.MyMethod()
{
MyMethod();
}
protected void MyMethod() // or public
{
// Do something ...
}
}
public class Bar : Foo, IMyInterface
{
public void MyMethod()
{
// Do something
base.MyMethod();
}
}
Exceptions
This rule does not report a violation for an explicit implementation of IDisposable.Dispose
when an externally visible
Close()
or System.IDisposable.Dispose(Boolean)
method is provided.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy