CA2222: Do not decrease inherited member visibility

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
RuleId CA2222
Category Microsoft.Usage
Breaking change Non-breaking

Cause

A private method in an unsealed type has a signature that is identical to a public method declared in a base type. The private method is not final.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

Don't change the access modifier for inherited members. Changing an inherited member to private does not prevent callers from accessing the base class implementation of the method. If the member is made private and the type is unsealed, inheriting types can call the last public implementation of the method in the inheritance hierarchy. If you must change the access modifier, either the method should be marked final or its type should be sealed to prevent the method from being overridden.

How to fix violations

To fix a violation of this rule, change the access to be non-private. Alternatively, if your programming language supports it, you can make the method final.

When to suppress warnings

Do not suppress a warning from this rule.

Example

The following example shows a type that violates this rule.

Imports System

Namespace UsageLibrary
Public Class ABaseType
   
   Public Sub BasePublicMethod(argument1 As Integer)
   End Sub 'BasePublicMethod
    
End Class 'ABaseType 

Public Class ADerivedType
   Inherits ABaseType
   
   ' Violates rule DoNotDecreaseInheritedMemberVisibility.
   Private Shadows Sub BasePublicMethod(argument1 As Integer)
   End Sub 'BasePublicMethod
End Class 'ADerivedType

End Namespace
using System;
namespace UsageLibrary
{
    public class ABaseType
    {
        public void BasePublicMethod(int argument1) {}
    }
    public class ADerivedType:ABaseType
    {
        // Violates rule: DoNotDecreaseInheritedMemberVisibility.
        // The compiler returns an error if this is overridden instead of new.
        private new void BasePublicMethod(int argument1){}       
    }
}