How to Resolve Error "CS0179: 'member' cannot be extern and declare a body" in C#
The Compiler Error CS0179 is a logical contradiction error. The message reads: " 'MethodName' cannot be extern and declare a body".
In C#, the extern modifier is used to indicate that a method is implemented externally, typically in unmanaged code (like a C++ DLL) referenced via [DllImport]. A method body (the code inside curly braces { ... }) indicates that the implementation is provided internally within the C# file.
These two concepts are mutually exclusive. You cannot tell the compiler to look for the code in an external file AND provide the code right there in the definition.
Understanding the extern Keyword
The extern modifier is almost exclusively used for Platform Invocation Services (P/Invoke). It tells the Common Language Runtime (CLR):
"Do not look for C# code for this method. When this method is called, load the specified DLL and run the function found there."
Because the code exists in the DLL, the C# signature serves only as a declaration (a pointer). It effectively functions like an abstract method or an interface member—it should end with a semicolon ;.
Scenario 1: Incorrect P/Invoke Declaration
This error usually happens when developers copy a P/Invoke signature but accidentally leave a placeholder body, or when they try to add "fallback logic" inside the extern method.
Example of error: attempting to call the Windows API MessageBox, but providing empty curly braces {}.
using System;
using System.Runtime.InteropServices;
public class NativeWrapper
{
// ⛔️ Error CS0179: 'MessageBox' cannot be extern and declare a body.
// The attribute says "Look in user32.dll".
// The body "{ return 0; }" says "Look here".
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, string text, string caption, int type)
{
return 0;
}
}
Solution 1: Remove the Method Body
If your intention is to call an external function (like a Windows API or a third-party C++ library), you must remove the implementation block entirely and replace it with a semicolon.
Solution:
using System;
using System.Runtime.InteropServices;
public class NativeWrapper
{
// ✅ Correct: Ended with a semicolon.
// The implementation is delegated to user32.dll.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, string text, string caption, int type);
}
Calling the External Method:
Once declared correctly, you call it like a normal static method:
NativeWrapper.MessageBox(IntPtr.Zero, "Hello", "Title", 0);
Solution 2: Remove the extern Modifier
If you did not intend to call external code and simply wanted to write a standard C# method, the extern keyword is the mistake. This often happens if you copy-paste code snippets without understanding the modifiers.
Solution: remove extern (and any [DllImport] attributes) and write your C# logic in the body.
public class CustomLogic
{
// ✅ Correct: A standard C# method with an implementation.
public static int Calculate(int a, int b)
{
return a + b;
}
}
Conclusion
CS0179 forces you to choose where your code lives.
- External Code: If using
[DllImport], useexternand end the line with;. - Internal Code: If writing C# logic, use
{ ... }and removeextern.