Compiler Error CS0501
'member function' must declare a body because it is not marked abstract, extern, or partial
Nonabstract methods must have implementations.
Explanation
In C#, methods/functions that are a part of a class must have a "body", or implementation. The compiler needs to know what should happen when these methods are called, so that it knows what to execute. A method with no body is not acceptable to the compiler because it wants to avoid confusion about the intent of the code.
There are exceptions to this rule:
- When the method is marked
abstract
as an Abstract Method - When the method is marked
extern
as an External Method - When the method is marked
partial
as a Partial Method
Example
The following sample generates CS0501:
public class MyClass
{
public void MethodWithNoBody(); // CS0501 declared but not defined
}
This could be fixed by declaring a body (by adding brackets):
public class MyClass
{
public void MethodWithNoBody() { } // No error; compiler now interprets as an empty method
}
Note
When defining a method body with brackets, do not add a semicolon. Doing so will trigger compiler error CS1597.
Or, using an appropriate keyword, such as defining an abstract
method:
abstract class MyClass // class is abstract; classes that inherit from it will have to define MyAbstractMethod
{
public abstract void MyAbstractMethod(); // Compiler now knows that this method must be defined by inheriting classes.
}
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for