Thursday, August 25, 2011

Use of Extern keyword

Use the extern modifier in a method declaration to indicate that the method is implemented externally. A common use of the extern modifier is with the DllImport attribute.

For example

using System; 
using System.Runtime.InteropServices; 
class MyClass 
{
     [DllImport("User32.dll")]    
     public static extern int MessageBox(int h, string m, string c, int type);   
     public static int Main()  
     {  
	     string myString;
             Console.Write("Enter your message: ");   
             myString = Console.ReadLine();
             return MessageBox(0, myString, "My Message Box", 0);   
     }
 }
 In this above example, the program receives a string from the user and displays it inside a message box. The program uses the MessageBox method imported from the User32.dll library  Extern is nothing but a memory storage type.If we declare extern before the function than the variable can be accessed by that function not only locally but externally too. 
 extern int a=4 
 Int fun (int x) 
 { 
    x=a;
}
  print gives a=4



4 comments:

Unknown said...

I also do agree with the stuff you shared, Thanks that's a wonderful post.

PIC Bonus

Unknown said...

Very informative article post. Fantastic.

PIC Scheme

Unknown said...

nice blog!

pic scheme singapore

Unknown said...

I like this post, enjoyed this one regards for posting .


PIC Claim

Post a Comment