How to Resolve Warning "CS0628: 'member' : new protected member declared in sealed class" in C#
The Compiler Warning CS0628 is an object-oriented design warning. The message reads: "'Member' : new protected member declared in sealed class".
In C#, the protected access modifier makes a member visible only to the containing class and its derived classes (children). However, the sealed modifier applied to a class explicitly prevents inheritance (no children allowed).
Defining a new protected member inside a sealed class creates a contradiction: you are opening access to child classes, while simultaneously forbidding child classes from existing. Consequently, the member is effectively private, but the syntax suggests you might have misunderstood the class's architecture.
This guide explains how to align your access modifiers with your class structure.
Understanding the Contradiction
sealedClass: "I cannot have derived classes."protectedMember: "I am accessible to derived classes."
When you introduce a new member with protected in a sealed class, no other class can ever utilize that protection level. From the outside world's perspective, the member behaves exactly like private. The compiler warns you because this usually implies you either didn't mean to seal the class, or you chose the wrong access modifier.
Scenario: Declaring Protected Members in Sealed Classes
This warning occurs when you define a method, property, or field as protected inside a class marked sealed.
Example of error:
public sealed class SecuritySystem
{
// ⛔️ Warning CS0628: 'SecuritySystem.ResetCore()' :
// new protected member declared in sealed class.
// Since 'SecuritySystem' is sealed, no class can inherit from it
// to access 'ResetCore'.
protected void ResetCore()
{
System.Console.WriteLine("System Reset");
}
}
Solution 1: Change to private (Internal Logic)
If the member was intended to be a helper method used only within the class itself, the correct modifier is private. Since no children can exist, private is semantically equivalent to protected in this context but logically correct.
Solution:
public sealed class SecuritySystem
{
// ✅ Correct: 'private' restricts usage to this class only.
private void ResetCore()
{
System.Console.WriteLine("System Reset");
}
public void PerformReset()
{
// Accessible here
ResetCore();
}
}
Solution 2: Change to public (External Access)
If you intended for other classes (outside the inheritance chain) to use this member, protected was the wrong choice to begin with. You likely meant public (visible to everyone) or internal (visible to the project).
Solution:
public sealed class SecuritySystem
{
// ✅ Correct: 'public' allows external callers to use this.
public void ResetCore()
{
System.Console.WriteLine("System Reset");
}
}
Exception: Overriding Inherited Members
There is one case where a protected member inside a sealed class is valid: when you are overriding a protected member defined in a base (parent) class.
In this case, you are not declaring a new member; you are fulfilling a contract defined by the parent. The parent defined the visibility, so the sealed child must respect it.
Valid Code (No Warning)
public class BaseDevice
{
// Defined in the parent
protected virtual void Initialize() { }
}
public sealed class Camera : BaseDevice
{
// ✅ Valid: This is an override, not a "new" member.
// We must keep 'protected' to match the signature of BaseDevice.
protected override void Initialize()
{
System.Console.WriteLine("Camera Init");
}
}
Conclusion
CS0628 is a logical consistency check.
- Check Inheritance: Is the class
sealed? - Check Intent:
- If the member is for internal use only: Change it to
private. - If the member is for external use: Change it to
publicorinternal. - If you really want inheritance: Remove the
sealedkeyword from the class.
- If the member is for internal use only: Change it to