Saturday, July 21, 2007

Extern Keyword Code Listing

Used to indicate that method is implemented outside the C# code
It is an error to use abstract and extern to modify the same member
Since extern method provides no implementation, there is no method body
E.g.: public static extern int extMethod(int x);

Code Listing: Using extern with User32.dll

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);
}
}

No comments:

Post a Comment