How to Resolve Error "CS0505: 'member1': cannot override because 'member2' is not a function" in C#
The Compiler Error CS0505 is a member type mismatch error. The message reads: "'Child.Member': cannot override because 'Parent.Member' is not a function".
In C#, the override keyword is used to provide a new implementation for a virtual member defined in a base class. However, you can only override members that contain logic: Methods, Properties, Indexers, or Events. You typically encounter this error when you try to override a member in the base class that is a Field (a variable) or a Constant.
This guide explains why fields cannot be overridden and how to resolve this naming conflict.
Understanding Overridable Members
Polymorphism (virtual/override) works by swapping pointers in a Virtual Method Table (vtable).
- Methods/Properties: These are instructions. The runtime can swap one set of instructions for another.
- Fields: These are raw memory storage. There is no "logic" to swap. Therefore, a field cannot be
virtual, and consequently, it cannot beoverriden.
CS0505 specifically happens when your child class defines a method or property marked override, but the parent class has a member with the same name that is just a simple variable (field).
Scenario: Attempting to Override a Field
A common mistake is defining a field in the parent class and trying to replace it with a method or property in the child class using the same name.
Example of error:
public class BaseClass
{
// This is a Field (variable). It cannot be virtual.
public string StatusString;
}
public class DerivedClass : BaseClass
{
// ⛔️ Error CS0505: 'DerivedClass.StatusString': cannot override because
// 'BaseClass.StatusString' is not a function.
public override string StatusString()
{
return "Active";
}
}
Solution 1: Use Properties (Virtual Logic)
If your goal is to allow derived classes to change how a value is retrieved, you should not use a Field in the base class. Use a Virtual Property. Properties look like fields to the outside world, but they compile into methods (get_X, set_X), which allows them to be overridden.
Solution: refactor the Base Class to use a Property.
public class BaseClass
{
// ✅ Correct: Defined as a virtual property.
public virtual string StatusString
{
get { return "Default"; }
}
}
public class DerivedClass : BaseClass
{
// ✅ Correct: Overriding a property with a property.
public override string StatusString
{
get { return "Active"; }
}
}
This is the standard Object-Oriented approach. It allows the Child to inject logic whenever StatusString is accessed, even if the object is held in a BaseClass variable.
Solution 2: Use Method Hiding (new)
If you cannot change the Base Class (e.g., it comes from a library) or if you simply want to reuse the name without actual polymorphism, you should use the new keyword instead of override. This is called Shadowing.
Solution: replace override with new.
public class BaseClass
{
public string StatusString; // Field
}
public class DerivedClass : BaseClass
{
// ✅ Correct: 'new' hides the field 'StatusString' from the parent
// and creates a brand new method with the same name.
public new string StatusString()
{
return "Active";
}
}
Behavior Warning:
DerivedClass d = new DerivedClass(); d.StatusString();calls the Method.BaseClass b = new DerivedClass(); b.StatusString = "Text";accesses the Field. The link between Parent and Child is broken.
Conclusion
CS0505 prevents you from treating storage (Fields) like logic (Methods).
- Check the Base: Look at the member in the parent class. Is it a
void Method()or aType Field;? - Polymorphism: If you want overriding, change the Base member to a
virtual property. - Shadowing: If you just want to reuse the name, change
overridetonewin the Child class.