Skip to content
Tutorials Center
My learning

Topics

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.

C#
[DllImport("libc", EntryPoint = "abs")]
static extern int NativeAbs(int value);

Try it

C# Calling a C library function

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.

C# A missing function fails at call time

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

C#
[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 extern method is implicitly non-virtual and must normally be static. 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 int is a C# int, a C long is 32 bits on Windows but 64 bits on 64-bit Linux (use CLong or nint for portable code), and pointers are IntPtr/nint. Wrong signatures corrupt the stack or crash the process.
  • Strings are marshalled by CharSet. Prefer CharSet.Unicode with the Windows W entry 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 a partial method, 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 SafeHandle and try/finally.
  • The binding is lazy. A program with a bad DllImport starts 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.dll or msvcrt.dll, so code that depends on specific libraries is not portable.