Rediger

Del via


extern (C# Reference)

Use the extern modifier to declare a method that's implemented externally. A common use of the extern modifier is with the DllImport attribute when you use Interop services to call into unmanaged code. In this case, you must also declare the method as static, as shown in the following example:

[DllImport("avifil32.dll")]
private static extern void AVIFileInit();

You can also use the extern keyword to define an external assembly alias. By using this alias, you can reference different versions of the same component from within a single assembly. For more information, see extern alias.

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

Tip

To find when a feature was first introduced in C#, consult the article on the C# language version history.

It's an error to use the abstract and extern modifiers together to modify the same member. Using the extern modifier means that the method is implemented outside the C# code, whereas using the abstract modifier means that the method implementation isn't provided in the class.

The extern keyword has more limited uses in C# than in C++. To compare the C# keyword with the C++ keyword, see Using extern to Specify Linkage in the C++ Language Reference.

In this example, the program receives a string from the user and displays it inside a message box. The program uses the MessageBox method imported from the User32.dll library.

//using System.Runtime.InteropServices;
class ExternTest
{
    [DllImport("User32.dll", CharSet=CharSet.Unicode)]
    public static extern int MessageBox(IntPtr h, string m, string c, int type);

    static int Main()
    {
        string myString;
        Console.Write("Enter your message: ");
        myString = Console.ReadLine();
        return MessageBox((IntPtr)0, myString, "My Message Box", 0);
    }
}

This example illustrates a C# program that calls into a C library (a native DLL).

  1. Create the following C file and name it cmdll.c:

    // cmdll.c
    // Compile with: -LD
    int __declspec(dllexport) SampleMethod(int i)
    {
      return i*10;
    }
    
  2. Open a Visual Studio x64 (or x86) Native Tools Command Prompt window from the Visual Studio installation directory and compile the cmdll.c file by typing cl -LD cmdll.c at the command prompt.

  3. In the same directory, create the following C# file and name it cm.cs:

    // cm.cs
    using System;
    using System.Runtime.InteropServices;
    public class MainClass
    {
        [DllImport("Cmdll.dll")]
          public static extern int SampleMethod(int x);
    
        static void Main()
        {
            Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
        }
    }
    
  4. Open a Visual Studio x64 (or x86) Native Tools Command Prompt window from the Visual Studio installation directory and compile the cm.cs file by typing:

    csc cm.cs (for the x64 command prompt) —or— csc -platform:x86 cm.cs (for the x86 command prompt)

    This command creates the executable file cm.exe.

  5. Run cm.exe. The SampleMethod method passes the value 5 to the DLL file, which returns the value multiplied by 10. The program produces the following output:

    SampleMethod() returns 50.
    

C# language specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also