Extren is mainly used to declare the external implementation method. What is the external implementation method? Generally, it is to use the System. Runtime. InteropServices service DllImport method to introduce the unmanaged code assembly. For example, calling program APIs and writing in C language. In this case, the declaration must be static
At the same time, the extren keyword can also define the external Assembly alias so that different versions of the same component can be referenced from a single assembly.
The following is a simple example of rewriting from MSDN. It calls the system winmm. DLL to play the wav file:
// System API call statement
[System. Runtime. InteropServices. DllImport ("winmm. DLL", EntryPoint = "PlaySound", SetLastError = true)]
Public static extern void PlaySound (string path, System. IntPtr hMod, PlaySoundFlags flags );
// Call this method
String path = @ "c: 22.wav ";
Try
{
PlaySound (path, new System. IntPtr (), PlaySoundFlags. SND_SYNC );
}
Catch (Exception ex)
{
Throw (ex );
}