extern(C# 参考)extern (C# Reference)

extern 修饰符用于声明在外部实现的方法。The extern modifier is used to declare a method that is implemented externally. extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 特性一起使用。A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code. 在这种情况下,还必须将方法声明为 static,如下面的示例所示:In this case, the method must also be declared as static, as shown in the following example:

private static extern void AVIFileInit();

extern 关键字还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。The extern keyword can also define an external assembly alias, which makes it possible to reference different versions of the same component from within a single assembly. 有关详细信息,请参阅外部别名For more information, see extern alias.

abstractextern 修饰符一起使用来修改同一成员是错误的做法。It is an error to use the abstract and extern modifiers together to modify the same member. 使用 extern 修饰符意味着方法是在 C# 代码的外部实现的,而使用 abstract 修饰符意味着类中未提供方法实现。Using the extern modifier means that the method is implemented outside the C# code, whereas using the abstract modifier means that the method implementation is not provided in the class.

extern 关键字用于 C# 中时会比用于 C++ 中时受到更多的限制。The extern keyword has more limited uses in C# than in C++. 若要比较 C# 关键字与 C++ 关键字,请参见 C++ 语言参考中的“使用 extern 指定链接”。To compare the C# keyword with the C++ keyword, see Using extern to Specify Linkage in the C++ Language Reference.

示例 1Example 1

在此示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。In this example, the program receives a string from the user and displays it inside a message box. 程序使用从 User32.dll 库导入的 MessageBox 方法。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);


示例 2Example 2

此示例阐释了调入 C 库(本机 DLL)的 C# 程序。This example illustrates a C# program that calls into a C library (a native DLL).

  1. 创建以下 C 文件并将其命名为 cmdll.cCreate the following C file and name it cmdll.c:
// cmdll.c
// Compile with: -LD
int __declspec(dllexport) SampleMethod(int i)
  return i*10;
  1. 从 Visual Studio 安装目录打开 Visual Studio x64(或 x32)本机工具命令提示符窗口,并通过在命令提示符处键入“cl -LD cmdll.c”来编译 cmdll.c 文件。Open a Visual Studio x64 (or x32) 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.

  2. 在相同的目录中,创建以下 C# 文件并将其命名为 cm.csIn the same directory, create the following C# file and name it cm.cs:

// cm.cs
using System;
using System.Runtime.InteropServices;
public class MainClass
      public static extern int SampleMethod(int x);

    static void Main()
        Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
  1. 从 Visual Studio 安装目录打开一个 Visual Studio x64(或 x32)本机工具命令提示符窗口,并通过键入以下内容来编译 cm.cs 文件:Open a Visual Studio x64 (or x32) Native Tools Command Prompt window from the Visual Studio installation directory and compile the cm.cs file by typing:

“csc cm.cs”(针对 x64 命令提示符)或“csc -platform:x86 cm.cs”(针对 x32 命令提示符)csc cm.cs (for the x64 command prompt) —or— csc -platform:x86 cm.cs (for the x32 command prompt)

这将创建可执行文件 cm.exeThis will create the executable file cm.exe.

  1. 运行 cm.exeRun cm.exe. SampleMethod 方法将值 5 传递到 DLL 文件,这将返回该值与 10 相乘后的结果。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# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also