extern C# with Example



extern C# with Example

The extern keyword is used to declare methods that are implemented externally. This can be used in conjunction 
with the DllImport attribute to call into unmanaged code using Interop services. which in this case it will come with 
static modifier 
For Example: 
using System.Runtime.InteropServices; 
public class MyClass 
{ 
[DllImport("User32.dll")] 
private static extern int SetForegroundWindow(IntPtr point); 
 

public void ActivateProcessWindow(Process p) 
{ 
SetForegroundWindow(p.MainWindowHandle); 
} 
} 
This uses the SetForegroundWindow method imported from the User32.dll library 
This can also be used to define an external assembly alias. which let us to reference different versions of same 
components from single assembly. 
To reference two assemblies with the same fully-qualified type names, an alias must be specified at a command 
prompt, as follows: 
/r:GridV1=grid.dll 
/r:GridV2=grid20.dll 
This creates the external aliases GridV1 and GridV2. To use these aliases from within a program, reference them by 
using the extern keyword. For example: 
extern alias GridV1; 
extern alias GridV2; 

0 Comment's

Comment Form