extern (C# 參考)extern (C# Reference)

extern 修飾詞是用來宣告於外部實作的方法。The extern modifier is used to declare a method that is implemented externally. extern 修飾詞的常見用法,是在使用 Interop 服務進行 Unmanaged 程式碼呼叫時,搭配 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:

[DllImport("avifil32.dll")]
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++ 關鍵字,請參閱<使用 extern 在 C++ 語言參考中指定連結>。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) Native Tools [命令提示字元] 視窗,並在命令提示字元中鍵入 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
{
    [DllImport("Cmdll.dll")]
      public static extern int SampleMethod(int x);

    static void Main()
    {
        Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
    }
}
  1. 從 Visual Studio 安裝目錄開啟 Visual Studio x64 (或 x32) Native Tools [命令提示字元] 視窗,並輸入下列內容來編譯 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

DllImportAttribute
C# 參考C# Reference
C# 程式設計指南C# Programming Guide
C# 關鍵字C# Keywords
修飾詞Modifiers