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

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...