C#
extern keyword
C# programming
interoperability
C# extern usage

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.

Browse interview questions

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.

csharp
1using System;
2using System.Runtime.InteropServices;
3
4class NativeMethods
5{
6    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
7    public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
8}

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:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        NativeMethods.MessageBox(IntPtr.Zero, "Hello", "Extern Demo", 0);
8    }
9}

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.

csharp
1using System.Runtime.InteropServices;
2
3class NativeMethods
4{
5    [DllImport("kernel32.dll")]
6    public static extern uint GetTickCount();
7}

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 extern by itself is enough without DllImport or 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# extern with the completely different extern keyword in the C language.

Summary

  • In C#, extern declares 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# extern questions are really interoperability questions.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.