C# Error CS0575 – Only class types can contain destructors

C# Compiler Error

CS0575 – Only class types can contain destructors

Reason for the Error

You will get this error in your C# code when you have used a destructor or finalizer for a struct.

For example, let’s compile the below C# program

using System;

namespace DeveloperPublishConsole1
{
    public struct Employee
    {
        // This will result in the error CS0575  
        ~Employee()   
        {
        }
    }
        internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

You will receive the error code CS0575 because you have a destructor specified for the struct Employee.

Error CS0575 Only class types can contain destructors DeveloperPublishConsole1 C:\Users\Senthil\source\repos\DeveloperPublishConsole1\DeveloperPublishConsole1\Program.cs 8 Active

C# Error CS0575 – Only class types can contain destructors

Solution

C# doesn’t allow a finalizer for a structure.

You can fix this error in your C# program by removing the finalizer that is defined for the struct.

Leave A Reply

Your email address will not be published. Required fields are marked *

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