Skip to main content

How to Resolve Error "CS0513: 'function' is abstract but it is contained in non-abstract class 'class'" in C#

The Compiler Error CS0513 is a class design consistency error. The message reads: "'MyMethod' is abstract but it is contained in non-abstract class 'MyClass'".

In C#, an abstract method is a method without an implementation (no code body). If a class contains even one abstract method, the class itself is considered incomplete. You cannot create an instance (new MyClass()) of a class that has missing parts. Therefore, C# enforces a strict rule: If a method is abstract, the containing class must also be marked as abstract.

This guide explains how to resolve this contradiction by either updating the class definition or providing a method body.

Understanding Abstract Classes

  • Concrete Class: A standard class (e.g., public class User). All methods must have bodies { ... }. You can create instances of this class.
  • Abstract Class: A partial class (e.g., public abstract class BaseUser). Can contain methods without bodies. You cannot create instances of this class directly; you must derive from it.

CS0513 occurs when you try to put a "signature-only" method inside a "fully concrete" class.

Scenario 1: Defining a Contract in a Concrete Class

This is the most common cause. You want to force derived classes to implement a specific method (like CalculateArea), so you declare it as abstract, but you forget to mark the class itself as abstract.

Example of the Code Causing the Error

// ⛔️ Error: The class 'Shape' is not abstract, 
// but it contains an abstract method 'CalculateArea'.
public class Shape
{
public int X { get; set; }
public int Y { get; set; }

// This method has no body, which makes it abstract.
public abstract double CalculateArea();
}

Solution

You have two options depending on your design goal:

Option A: Make the Class Abstract

If Shape is indeed a base concept that should never be instantiated directly, mark the class as abstract.

// ✅ Correct: The class is now abstract, matching the method.
public abstract class Shape
{
public int X { get; set; }
public int Y { get; set; }

public abstract double CalculateArea();
}

Option B: Provide a Default Implementation

If Shape should be instantiated directly, remove the abstract keyword from the method and provide a default code body. You can use virtual to allow overrides later.

public class Shape
{
public int X { get; set; }
public int Y { get; set; }

// ✅ Correct: Removed 'abstract', added 'virtual' and a body { }.
public virtual double CalculateArea()
{
return 0.0; // Default behavior
}
}

Scenario 2: Accidentally Copying Interface Syntax

Sometimes developers copy method signatures from an Interface into a Class intending to implement them, but leave the semicolon ; at the end instead of adding braces { }. In C#, a method ending in a semicolon inside a class is interpreted as an attempt to define an abstract method (or an external one), which triggers this error if the abstract keyword is present or implied contextually.

Example of the Code Causing the Error

public class DataService
{
// ⛔️ Error CS0513 (or CS0501 "must declare a body"):
// The syntax implies an abstract definition inside a concrete class.
public abstract void SaveData();
}

Solution

If you intended to implement the method, remove the abstract keyword and replace the semicolon with curly braces containing your logic.

public class DataService
{
// ✅ Correct: A standard method with a body.
public void SaveData()
{
System.Console.WriteLine("Data saved.");
}
}

Conclusion

CS0513 ensures that you don't create "broken" classes that cannot be instantiated.

  1. Check the Method: Does it have the abstract keyword?
  2. Check the Class: Does it have the abstract keyword?
  3. Fix:
    • If you want a base class template: Add abstract to the class definition.
    • If you want a standard class: Remove abstract from the method and add a body { }.