Uredi

Deli z drugimi prek


CA1033: Interface methods should be callable by child types

Property Value
Rule ID CA1033
Title Interface methods should be callable by child types
Category Design
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 8 No

Cause

An unsealed externally visible type provides an explicit method implementation of a public interface and does not provide an alternative externally visible method that has the same name.

Rule description

Consider a base type that explicitly implements a public interface method. A type that derives from the base type can access the inherited interface method only through a reference to the current instance (this in C#) that is cast to the interface. If the derived type reimplements (explicitly) the inherited interface method, the base implementation can no longer be accessed. The call through the current instance reference will invoke the derived implementation; this causes recursion and an eventual stack overflow.

This rule does not report a violation for an explicit implementation of System.IDisposable.Dispose when an externally visible Close() or System.IDisposable.Dispose(Boolean) method is provided.

How to fix violations

To fix a violation of this rule, implement a new method that exposes the same functionality and is visible to derived types or change to a nonexplicit implementation. If a breaking change is acceptable, an alternative is to make the type sealed.

When to suppress warnings

It is safe to suppress a warning from this rule if an externally visible method is provided that has the same functionality but a different name than the explicitly implemented method.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1033
// The code that's violating the rule is on this line.
#pragma warning restore CA1033

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1033.severity = none

For more information, see How to suppress code analysis warnings.

Example

The following example shows a type, ViolatingBase, that violates the rule and a type, FixedBase, that shows a fix for the violation.

public interface ITest
{
    void SomeMethod();
}

public class ViolatingBase : ITest
{
    void ITest.SomeMethod()
    {
        // ...
    }
}

public class FixedBase : ITest
{
    void ITest.SomeMethod()
    {
        SomeMethod();
    }

    protected void SomeMethod()
    {
        // ...
    }
}

sealed public class Derived : FixedBase, ITest
{
    public void SomeMethod()
    {
        // The following would cause recursion and a stack overflow.
        // ((ITest)this).SomeMethod();

        // The following is unavailable if derived from ViolatingBase.
        base.SomeMethod();
    }
}

See also