How to Resolve Error "CS0574: Name of destructor must match name of type" in C#
The Compiler Error CS0574 is a syntax error regarding Destructors (also known as Finalizers). The message reads: "Name of destructor must match name of type".
In C#, a destructor is a special method used to perform cleanup before an object is reclaimed by the Garbage Collector. The syntax for a destructor is a tilde ~ followed immediately by the class name. The rule is strict: The name after the tilde must exactly match the name of the class containing it.
This error occurs when you rename a class but forget to rename its destructor, or when you copy-paste code from one class to another without updating the name.
Understanding Destructor Syntax
In C++, destructors are common. In C#, they are rare and technically called Finalizers.
The syntax is:
class MyClass
{
// The Finalizer
~MyClass()
{
// Cleanup unmanaged resources
}
}
If the class is named MyClass, the finalizer must be named ~MyClass. It cannot be ~MyClass2, ~OtherClass, or ~myClass (lowercase).
Scenario 1: Renaming or Copy-Paste Errors
The most common cause of CS0574 is refactoring. You rename a class using a simple "Find and Replace" or manual typing, but you miss the destructor definition.
Example of error
Here, the class is named FileManager, but the destructor is still named ~FileHandler.
public class FileManager
{
public FileManager()
{
System.Console.WriteLine("Created");
}
// ⛔️ Error CS0574: Name of destructor must match name of type.
// The class is 'FileManager', but this looks like a destructor for 'FileHandler'.
~FileHandler()
{
System.Console.WriteLine("Destroyed");
}
}
Solution: Rename to Match
Update the destructor name to match the class exactly.
public class FileManager
{
public FileManager()
{
System.Console.WriteLine("Created");
}
// ✅ Correct: The name matches the class.
~FileManager()
{
System.Console.WriteLine("Destroyed");
}
}
Scenario 2: Case Sensitivity
C# is a case-sensitive language. Logger and logger are different identifiers. The destructor name must match the casing of the class name exactly.
Example of error
public class Logger
{
// ⛔️ Error CS0574: Name of destructor must match name of type.
// 'logger' (lowercase) does not match 'Logger' (uppercase).
~logger()
{
// ...
}
}
Solution: Fix Casing
public class Logger
{
// ✅ Correct: Exact case match.
~Logger()
{
// ...
}
}
Best Practice: Do You Need a Destructor?
Before fixing this error, ask yourself: Do I actually need a destructor?
In modern C#, destructors (finalizers) are discouraged unless you are directly wrapping an unmanaged resource (like a raw C++ pointer, a file handle without a SafeHandle wrapper, or raw window handles).
If you are using a destructor just to clean up managed objects (like closing a database connection or clearing a list), you are doing it wrong. This can actually hurt performance.
Recommended Alternative: IDisposable
Instead of a destructor, implement the IDisposable interface.
using System;
// ✅ Correct: Use IDisposable for standard cleanup
public class FileManager : IDisposable
{
public void Dispose()
{
// Clean up resources here
Console.WriteLine("Resources released");
}
}
If you implement IDisposable, you usually don't need a destructor ~ClassName unless you are also holding unmanaged memory that needs to be freed if the developer forgets to call Dispose().
Conclusion
CS0574 is a simple naming mismatch.
- Check the Class Name: Look at the
class Namedefinition. - Check the Tilde: Look at the
~Namemethod. - Match Them: Ensure they are identical, including capitalization.
- Evaluate Necessity: Consider removing the destructor entirely in favor of
IDisposableif you are not handling unmanaged code.