A method declaration may include the extern
modifier to indicate that the method is implemented externally. Because an external method declaration provides no actual implementation, the method-body of an external method simply consists of a semicolon.
The extern
modifier is typically used in conjunction with a DllImport
attribute (§20.1), allowing external methods to be implemented by DLLs (Dynamic Link Libraries). The execution environment may support other mechanisms whereby implementations of external methods can be provided.
It is an error for an external method declaration to also include the abstract
modifier. When an external method includes a DllImport
attribute, the method declaration must also include a static
modifier.
This example demonstrates use of the extern
modifier and the DllImport
attribute:
class Path { [DllImport("kernel32", setLastError=true)] static extern bool CreateDirectory(string name, SecurityAttributes sa); [DllImport("kernel32", setLastError=true)] static extern bool RemoveDirectory(string name); [DllImport("kernel32", setLastError=true)] static extern int GetCurrentDirectory(int bufSize, StringBuilder buf); [DllImport("kernel32", setLastError=true)] static extern bool SetCurrentDirectory(string name); }