How to Resolve Error "CS0506: cannot override inherited member because it is not marked "virtual", "abstract", or "override"" in C#
The Compiler Error CS0506 is an inheritance permission error. The message reads: " 'Child.Method' : cannot override inherited member 'Parent.Method' because it is not marked virtual, abstract, or override".
In C#, methods are sealed by default. This means that unless the creator of a base class explicitly marks a method as "overridable" (using virtual or abstract), derived classes are strictly forbidden from changing its behavior using the override keyword. This design choice ensures that base classes maintain control over their core logic unless they opt-in to polymorphism.
This guide explains how to enable overriding or how to work around locked methods.
Understanding Default Inheritance Rules
When you inherit from a class, you get all its public/protected behavior.
- Standard Method (
void DoWork()): The behavior is fixed. The child class inherits it as-is. - Virtual Method (
virtual void DoWork()): The behavior is a default suggestion. The child class can replace it.
If you try to use override on a Standard Method, the compiler raises CS0506 because it cannot find the "Virtual Method Table" entry required to perform the swap.
Scenario 1: Overriding a Standard Method
This generally happens when you want to specialize a specific behavior in a subclass, but the original author of the base class defined the method as a normal, non-virtual method.
Example of error:
public class Logger
{
// A standard method. Not marked virtual.
public void Log(string message)
{
Console.WriteLine($"[Log]: {message}");
}
}
public class FileLogger : Logger
{
// ⛔️ Error CS0506: 'FileLogger.Log(string)': cannot override inherited member
// 'Logger.Log(string)' because it is not marked virtual, abstract, or override.
public override void Log(string message)
{
File.WriteAllText("log.txt", message);
}
}
Solution 1: Make the Base Method virtual (Recommended)
If you have access to the source code of the base class, and it makes logical sense for that method to be customized by children, add the virtual keyword.
Solution: update the parent class definition.
public class Logger
{
// ✅ Correct: Added 'virtual' to allow overriding
public virtual void Log(string message)
{
Console.WriteLine($"[Log]: {message}");
}
}
public class FileLogger : Logger
{
// ✅ Correct: 'override' now works because the base is virtual
public override void Log(string message)
{
File.WriteAllText("log.txt", message);
}
}
Polymorphism: With this solution, if you cast FileLogger to Logger, calling .Log() will correctly run the FileLogger version.
Solution 2: Use the new Keyword (Shadowing)
If you cannot modify the base class (e.g., it comes from a third-party DLL or the .NET Framework), you physically cannot use override. Instead, you must use the new keyword.
This creates a brand new method that "hides" the parent's version.
Solution: replace override with new.
public class Logger
{
// We cannot change this file (it might be in a library)
public void Log(string message) { /*...*/ }
}
public class FileLogger : Logger
{
// ✅ Correct: We declare a NEW method that happens to share the name.
public new void Log(string message)
{
File.WriteAllText("log.txt", message);
}
}
Important Behavior Difference:
FileLogger f = new FileLogger(); f.Log("Hi");-> Runs FileLogger version.Logger l = new FileLogger(); l.Log("Hi");-> Runs Logger (Base) version. Because it is not an override, polymorphism does not apply.
Conclusion
CS0506 enforces the "Open/Closed Principle" at the compiler level.
- Check Source Access: Can you edit the Base Class?
- Yes: Change the base method to
virtual. - No: Change your child method from
overridetonew.
- Yes: Change the base method to
- Verify Intent: Are you sure you are overriding the right method? Sometimes this error appears if you misspelled the name of a method you thought was virtual, but matched a non-virtual one instead.