Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0509 – ‘class1’ : cannot derive from sealed type ‘class2’

C# Compiler Error

CS0509 – ‘class1’ : cannot derive from sealed type ‘class2’

Reason for the Error

You’ll get this error in your C# code when you attempt to try to treat a sealed class as a base class and inherit from it.

For example, let’s try to compile the below C# code snippet.

using System;
namespace DeveloperPublishNamespace
{
    sealed public class BaseClass
    {
    }

    public class ChildClass : BaseClass
    {
    }
    class Program
    {
      
        static void Main(string[] args)
        {
            Console.WriteLine("No Error");
        }
    }
}

You’ll receive the error code CS0509 because the class BaseClass is marked as sealed and you are trying to inherit the ChildClass from the sealed base class.

Error CS0509 ‘ChildClass’: cannot derive from sealed type ‘BaseClass’ DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 8 Active

C# Error CS0509 – 'class1' : cannot derive from sealed type 'class2'

Solution

sealed classes in C# cannot act as a base class in C#. You will need to remove the sealed modifier if you need to inherit from this class. Also note that by default, structs in C# are sealed.