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

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.

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...