CA1047: Do not declare protected members in sealed types

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

TypeNameDoNotDeclareProtectedMembersInSealedTypes
CheckIdCA1047
CategoryMicrosoft.Design
Breaking ChangeNon-breaking

A public type is sealed (NotInheritable in Visual basic) and declares a protected member or a protected nested type. This rule does not report violations for Finalize methods, which must follow this pattern.

Types declare protected members so that inheriting types can access or override the member. By definition, you cannot inherit from a sealed type, which means that protected methods on sealed types cannot be called.

The C# compiler issues a warning for this error.

To fix a violation of this rule, change the access level of the member to private, or make the type inheritable.

Do not suppress a warning from this rule. Leaving the type in its current state can cause maintenance issues and does not provide any benefits.

The following example shows a type that violates this rule.

using System;

namespace DesignLibrary
{
   public sealed class SealedClass
   {
      protected void ProtectedMethod(){}
   }
}

Show: