Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Development Edition
Design Warnings
 Do not declare protected members in...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio Team System
Do not declare protected members in sealed types

Updated: November 2007

TypeName

DoNotDeclareProtectedMembersInSealedTypes

CheckId

CA1047

Category

Microsoft.Design

Breaking Change

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

Visual Basic
Imports System

Namespace DesignLibrary

   Public NotInheritable Class BadSealedType
      Protected  Sub MyMethod
      End Sub
   End Class

End Namespace

C#
using System;

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

The above sealed type declares a protected member, which cannot be called outside the class that declares it.

If the method was designed to be called by other types, increase its accessibility to public, otherwise, reduce its accessibility to private.

The following example fixes the above violation by increasing the method's accessibility to public.

Visual Basic
Imports System

Namespace Samples

   Public NotInheritable Class Book
        Protected Sub Read      
    End Sub

   End Class

End Namespace

C#
using System;

namespace Samples
{    
    public sealed class Book     
    {        
        protected void Read()        
        {        
        }    
    }
}

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
As of v1.35, fxcop DOES fire for sealed classes with protected overrides from base classes in c#:      AndyDragon_ca   |   Edit   |  
class Foo
{
    protected virtual void DoSomething()
    {
    }
}
 
sealed class Bar : Foo
{
    protected override void DoSomething()
    {
        base.DoSomething();
    }
}

This will fire the event when building with fxcop 1.35, a suppression is required to not fire the violation.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker