C# Reference ยท Keywords and Statements
The extern keyword
Reference for the C# extern keyword: declare methods implemented in native libraries with DllImport, and use extern alias to separate assembly versions.
extern has two unrelated uses. On a method, it says "the implementation lives somewhere else": usually a function in a native shared library (.dll on Windows, .so on Linux, .dylib on macOS). You combine it with static and an attribute such as [DllImport], and the runtime resolves the native function the first time you call it. This mechanism is called platform invoke, or P/Invoke.
The second use, extern alias, tells the compiler about a name for an assembly reference so that two assemblies containing the same fully qualified type names can be used side by side.
[DllImport("libc", EntryPoint = "abs")] static extern int NativeAbs(int value);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[DllImport("library", EntryPoint = "name", CharSet = CharSet.Unicode)] static extern ReturnType Method(ParameterTypes ...); extern alias Name; // at the top of a source file using Lib1 = Name::Namespace;
| Part | Description |
|---|---|
DllImport(library) |
Name of the native library. The runtime adds the platform prefix and suffix and searches the usual paths. |
EntryPoint |
Native function name, when it differs from the C# method name. |
CharSet, SetLastError, CallingConvention |
Optional marshalling settings for strings, error codes and the calling convention. |
extern alias |
Alias for an assembly reference declared with /reference:Name=file.dll or <Aliases> in the project. |
Errors: a native library that cannot be found throws DllNotFoundException; a missing function throws EntryPointNotFoundException. Giving an extern method a body fails with CS0179; leaving out the attribute that names the library gives warning CS0626.
Usage notes
- An
externmethod is implicitly non-virtual and must normally bestatic. It has no body, and the compiler warns (CS0626) when you forget the attribute that tells the runtime where to find the code. - Matching types matters: a C
intis a C#int, a Clongis 32 bits on Windows but 64 bits on 64-bit Linux (useCLongornintfor portable code), and pointers areIntPtr/nint. Wrong signatures corrupt the stack or crash the process. - Strings are marshalled by
CharSet. PreferCharSet.Unicodewith the WindowsWentry points, and UTF-8 ([MarshalAs(UnmanagedType.LPUTF8Str)]) on Linux and macOS. - .NET 7+ offers
[LibraryImport], a source-generated version that avoids run-time stub generation and is trimming/AOT friendly. It needs a source generator and apartialmethod, so it is not available on this site's runner. - Native calls skip C#'s safety: no bounds checks, no garbage collection of native memory. Release handles with
SafeHandleandtry/finally. - The binding is lazy. A program with a bad
DllImportstarts normally and only fails when the method is first called. - This example uses libc, which is present on Linux. On Windows you would import
kernel32.dllormsvcrt.dll, so code that depends on specific libraries is not portable.