How does extern work in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, extern means that a method is declared in your code but implemented elsewhere. The most common use is platform invocation, where managed C# code declares a method and the actual implementation lives in an unmanaged DLL.
extern Declares an External Implementation
An extern method has no body in C#. Instead, the runtime finds the implementation from external metadata such as DllImport.
The extern keyword tells the compiler that the method body is not in C# source. The DllImport attribute tells the runtime where to find it.
Typical P/Invoke Usage
You then call the method like a normal static method:
Even though the call looks ordinary in C#, the actual implementation comes from native Windows code.
Why extern Matters
Without extern, the compiler would expect a method body in your C# file. With extern, you are declaring a signature only.
That is why extern is common in interoperability scenarios:
- Windows API calls,
- native libraries,
- legacy unmanaged components,
- platform-specific system functions.
The keyword is less about syntax and more about telling the compiler and runtime how to treat the declaration.
That is why extern usually appears together with other interop details rather than as a standalone language feature you use every day in ordinary business logic.
Signature Accuracy Is Critical
When you use extern with P/Invoke, the method signature must match the native function closely enough for arguments and return values to marshal correctly.
If types, calling conventions, or character sets are wrong, the call may fail or even corrupt memory-related behavior at runtime.
Interop bugs can be frustrating because the C# code may compile perfectly while the failure only appears when the native boundary is crossed.
extern Is Not Just About Native Code
C# also has another feature named extern alias, used to distinguish assemblies with conflicting type names. That is a different use of the word extern from P/Invoke, but it reinforces the same idea: the referenced code lives outside the current source unit.
Most everyday questions about extern in C#, though, are really about unmanaged interop.
So when you see extern, the next question is usually "where is the implementation actually coming from?"
That question usually leads you to the real debugging path.
And that is usually where the real answer lives.
It rarely stays a purely syntactic question.
Common Pitfalls
- Thinking
externby itself is enough withoutDllImportor other supporting metadata. - Declaring the wrong parameter types for the external function.
- Forgetting about character encoding when native APIs expect Unicode or ANSI strings.
- Treating native calls like ordinary managed code and skipping error handling.
- Confusing C#
externwith the completely differentexternkeyword in the C language.
Summary
- In C#,
externdeclares that a method is implemented outside the current C# source. - The common use case is P/Invoke with
DllImport. - The method looks normal to the caller, but the implementation lives in an external DLL.
- Accurate signatures are essential for safe interoperation.
- Most practical C#
externquestions are really interoperability questions.
Related reading
- How does MVC 4 List Model Binding work?
- How does .NET framework allocate memory for OutOfMemoryException?
- How does one tell if an IDisposable object reference is disposed?
- How does one test async code using MSTest
- How does RegexOptions.Compiled work?
- How does String.Equalsa,b not produce a StackOverflowException?
- How does string.Format handle null values?
- How does Taskint become an int?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.