Here is the relevant paragraph from the ECMA C# Language Specification
that you can download at http://msdn.microsoft.com/net/ecma/:
"When a method declaration includes an extern modifier, the method is
said to be an external method. External methods are implemented
externally, typically using a language other than C#. Because an
external method declaration provides no actual implementation, the
method-body of an external method simply consists of a semicolon."
The extern keyword is simply saying to the C# compiler that the
implementation of the method will be provided by somebody else. The most
common usage of the extern keyword is in the pinvoke declarations like:
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern bool CloseHandle(IntPtr handle);
In this case and the case you have mentioned the CLR synthesizes the
method implementation based on the attributes.
The usage of the extern keyword is not necessarily limited to things
that are known to CLR itself. You can compile C# program with extern
methods and then run some post processing tool that will replace the
extern methods with an IL implementation. A lot of various hacks can be
done this way:
- You can auto generate implementations for methods that are otherwise
mechanical to write.
- You can merge module implemented in C# with module implemented in some
other language targeting CLI into one. The extern method would be used
to describe the interfacing points.
- Or you can inject arbitrary IL sequences into the C# program. A neat
example could be a post processing tool that overcomes the lack of
inline IL assembly in C#. You could have:
[ILASM("ldarg 0\n ckfinite\n ret")]
static extern bool IsFinite(double x);
The post processing tool can run ildasm, look for ILASMAttribute with
replacing things as appropriate, and then run ilasm again.
-Jan
This posting is provided "AS IS" with no warranties, and confers no
rights.
-----Original Message-----
From: Joe Mayo [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 20, 2003 6:40 PM
To: [EMAIL PROTECTED]
Subject: [DOTNET-ROTOR] extern keyword
I was discussing the extern keyword on another list and someone showed
me this:
/// <include file='doc\Assembly.uex'
path='docs/doc[@for="Assembly.CreateQualifiedName"]/*' />
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern String CreateQualifiedName(String
assemblyName, String typeName);
In this usage, what is the extern keyword doing?
Are there any other ways the extern keyword is used that isn't
documented?
Joe