How to Resolve Error "CS0423: Since 'class' has the ComImport attribute, 'method' must be extern or abstract" in C#
The Compiler Error CS0423 is a definition error related to COM Interoperability (Component Object Model). The message reads: "Since 'MyClass' has the ComImport attribute, 'MyMethod' must be extern or abstract".
In C#, the [ComImport] attribute marks a class as a definition that maps to an external COM class (usually defined in a Type Library or DLL registered in Windows). Because the implementation of this class exists outside of the .NET runtime (in the unmanaged COM world), you cannot provide C# method bodies (implementation) for it. All methods inside such a class must be definitions only, marked as either extern or abstract.
This guide explains how to correctly define COM wrapper classes.
Understanding [ComImport] Classes
When you apply [ComImport] to a class, you are telling the compiler:
"This class definition is just a shell. The real logic lives in an external COM component identified by a Guid."
Since the runtime expects to look up the implementation via the COM subsystem, it is illogical for you to write C# code { ... } inside the methods of this class. The compiler enforces this by requiring methods to be signatures only.
Scenario: Providing a Method Body
This error occurs when you try to mix a COM definition with standard C# implementation logic.
Example of error:
using System;
using System.Runtime.InteropServices;
[ComImport]
[Guid("00000000-0000-0000-0000-000000000001")] // Example Guid
public class LegacyComObject
{
// ⛔️ Error CS0423: Since 'LegacyComObject' has the ComImport attribute,
// 'DoSomething' must be extern or abstract.
// You cannot provide a body { } here.
public void DoSomething()
{
Console.WriteLine("Trying to implement logic...");
}
}
Solution: Use extern or abstract
To fix this, you must remove the method body and mark the method appropriately. The standard way for COM definitions in C# is to use extern.
Solution:
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[ComImport]
[Guid("00000000-0000-0000-0000-000000000001")]
public class LegacyComObject
{
// ✅ Correct: 'extern' tells the compiler the implementation is elsewhere.
// Note: Method bodies are removed.
public extern void DoSomething();
// You can also use 'abstract' in specific interface-like scenarios,
// but 'extern' is standard for [ComImport] classes.
}
Interfaces vs. Classes:
Usually, COM Interop involves interfaces ([ComImport, InterfaceType(...)]). In interfaces, methods effectively have no body by default. CS0423 specifically targets Classes marked with [ComImport], which are less common than interfaces but used for CoClasses.
Conclusion
CS0423 ensures that you don't write "ghost code" that will never run.
- Check the Attribute: Does the class have
[ComImport]? - Check the Methods: Do any methods have curly braces
{ ... }? - Fix: Remove the braces/code and add the
externkeyword to the method signature.