Skip to main content

How to Resolve Error "CS0509: 'class1' : cannot derive from sealed type 'class2'" in C#

The Compiler Error CS0509 is an inheritance restriction error. The message reads: "'ChildClass' : cannot derive from sealed type 'ParentClass'".

In C#, the sealed modifier is used to prevent other classes from inheriting from a class. It effectively declares that a class is "complete" or "final" and should not be extended further. If you attempt to use a sealed class as a base class (using the : syntax), the compiler raises CS0509 because the class explicitly forbids it.

This guide explains the purpose of sealed classes and how to extend functionality when inheritance is blocked.

Understanding the sealed Modifier

Developers mark classes as sealed for several reasons:

  • Security: To prevent tampering with critical logic.
  • Performance: The runtime can optimize method calls (devirtualization) if it knows a method cannot be overridden.
  • Design: To indicate that a class is specialized and not designed to be a base class.

Once a class is sealed, the inheritance chain stops.

Scenario 1: Inheriting from User-Defined Sealed Classes

This occurs when you (or a teammate) marked a class as sealed in your own project, but later tried to create a subclass.

Example of error:

// Ideally, this class is "Final"
public sealed class SecurityToken
{
public string Key { get; set; }
}

// ⛔️ Error CS0509: 'AdvancedToken' : cannot derive from sealed type 'SecurityToken'
public class AdvancedToken : SecurityToken
{
public int Expiry { get; set; }
}

Scenario 2: Inheriting from .NET Framework Classes

Many standard .NET types are sealed. For example, System.String, System.Int32, and System.Tuple are sealed. You physically cannot create a class that inherits from string.

The Mistake

// ⛔️ Error CS0509: 'MyString' : cannot derive from sealed type 'string'
public class MyString : System.String
{
// ...
}

Solution 1: Remove the sealed Modifier

If you own the source code of the base class (as in Scenario 1) and you decide that inheritance is actually necessary, simply delete the keyword.

Solution:

// ✅ Correct: Removed 'sealed'
public class SecurityToken
{
public string Key { get; set; }
}

// Now this works
public class AdvancedToken : SecurityToken
{
public int Expiry { get; set; }
}
warning

Before unsealing a class, check why it was sealed. If it contains sensitive security logic, allowing inheritance might introduce vulnerabilities.

Solution 2: Use Composition (Wrapper Pattern)

If you cannot change the base class (because it is part of .NET or a third-party library), you should use Composition instead of Inheritance. This means creating a new class that contains an instance of the sealed class ("Has-a" relationship) rather than being an instance of it ("Is-a" relationship).

Solution (Wrapper): instead of inheriting from string (which is impossible), wrap it.

public class SmartString
{
// 1. Hold the sealed class as a field/property
private string _value;

public SmartString(string initialValue)
{
_value = initialValue;
}

// 2. Expose the functionality you need
public int Length => _value.Length;

// 3. Add your new functionality
public bool IsPalindrome()
{
// Custom logic here
return false;
}

// Optional: Implicit conversion back to string
public static implicit operator string(SmartString s) => s._value;
}

Solution 3: Use Extension Methods

If your goal is simply to add methods to a sealed class (like adding Reverse() to string), you do not need inheritance at all. Use Extension Methods.

Solution (Extension):

public static class StringExtensions
{
// ✅ Correct: Adds a method to the sealed 'string' class visually.
public static string Reverse(this string input)
{
char[] charArray = input.ToCharArray();
System.Array.Reverse(charArray);
return new string(charArray);
}
}

// Usage:
// string s = "hello";
// string r = s.Reverse(); // Looks like it's part of the class

Conclusion

CS0509 protects the integrity of classes designed to be final.

  1. Check Ownership: Can you edit the base class?
    • Yes: Remove sealed if inheritance is safe and desired.
    • No: You cannot inherit. Use alternatives.
  2. Use Composition: Wrap the sealed class in your own class.
  3. Use Extensions: If you just want to add utility methods, use Extension Methods (this TypeName variable).