Whenever I look deeply enough into reflector I bump into extern
methods with no source. I read the msdn documentation at http://msdn.microsoft.com/en-us/library/e59b22c5(v=vs.80).aspx. What I got from that article is that methods with the extern
modifier have to be injected. I interpreted this to mean it works something like an abstract factory pattern. I also noticed that I've never seen a non-static extern method. Is static declaration a requirement (I could see how this would make sense)? I'm still guessing here and I'm not sure how it actually works. It seems to me like the compiler must recognize certain attributes that mitigate processing, but I don't know what the attributes are other than ones I've come across like MethodImplAttribute
and DllImportAttribute
from the MSDN example. How does someone leverage the extern
attribute? It said that in many instances this can increase performance. Also, how would I go about looking into the source of extern
methods like Object.InternalGetEquals()
?

- 41,281
- 29
- 127
- 212
-
1`MemberwiseClone()` method on `System.Object` class in .Net is `extern` but NOT `static`. – RBT Dec 26 '16 at 04:06
-
1When an extern method includes a `DllImport` attribute, then the method declaration must also include a static modifier. – RBT Dec 26 '16 at 04:43
4 Answers
Consider reading section 10.6.7 of the C# specification, which answers many of your questions. I reproduce part of it here for your convenience:
When a method declaration includes an extern modifier, that 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. An external method may not be generic. The extern modifier is typically used in conjunction with a DllImport attribute, allowing external methods to be implemented by DLLs (Dynamic Link Libraries). The execution environment may support other mechanisms whereby implementations of external methods can be provided. When an external method includes a DllImport attribute, the method declaration must also include a static modifier.
How does someone leverage the extern attribute?
- Write your code in the unmanaged language of your choice.
- Compile it into a DLL, exporting the entry point of your code.
- Make an interop library that defines the method as an extern method in the given DLL.
- Call it from C#.
- Profit!
How would I go about looking into the source of extern methods like Object.InternalGetEquals()?

- 647,829
- 179
- 1,238
- 2,067
-
8The SSLI20 source code is still a pretty accurate copy of the internal methods for mere mortals like me. Good enough to answer a bunch of CLR questions anyway. Look at clr/src/vm/ecall.cpp for the mapping from the framework name to the C++ function. Download is here: http://www.microsoft.com/downloads/en/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en – Hans Passant Feb 24 '11 at 21:54
-
2@Hans: please note that after looking at SSCLI you're not allowed to contribute to Mono project. – Dan Abramov Jun 04 '11 at 17:07
-
1Hmm, you'd better stay away from answers by Microsoft employees too. – Hans Passant Jun 04 '11 at 18:24
-
@Eric, why does an external method may not be generic? I've noticed that the C# compiler compiles generic external methods just fine. I'm working on a [post-compiler](http://codecrafter.blogspot.com/2011/05/nroles-experiment-with-roles-in-c.html) that inserts methods in classes. So I was thinking about using `extern` as a placeholder for the post-compiler to insert code. I don't think `extern` was designed _only_ to be used with `[DllImport]`, was it? – Jordão Jun 07 '11 at 14:42
-
@Jordão: If the method is external to the runtime then *how does the jitter know how to compile the constructed method*? – Eric Lippert Jun 07 '11 at 15:02
-
@Eric: The post-compiler runs _before_ the jitter, just _after_ compilation. It's not a runtime API, it just changes the generated assembly. – Jordão Jun 07 '11 at 15:29
-
@Eric: to me it's not external to the _runtime_, but external to the _C# compiler_. Isn't that what it's _supposed_ to mean? – Jordão Jun 07 '11 at 15:40
-
@Jordão: Read the bit of the spec I quoted again. External methods are *methods that are implemented externally*. The spec does not say "external methods are a general-purpose language extension point for post-compilers". It says that external methods are a way to indicate to the compiler that the implementation of this method is somewhere outside of the managed system. – Eric Lippert Jun 07 '11 at 16:44
-
1@Eric: thanks for the explanation. I see what's their purpose now. I just wanted to interpret it a little more broadly to _encompass_ my scenario, not to _replace_ the original scenario with mine. Kind of like how people _abuse_ the `using` statement. – Jordão Jun 07 '11 at 17:03
Methods marked extern
with [DllImport]
attribute are usually calls to C libraries. This feature is useful for calling WinAPI or legacy code.
This is example from MSDN:
using System;
using System.Runtime.InteropServices;
class MainClass
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
static int Main()
{
string myString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
return MessageBox(0, myString, "My Message Box", 0);
}
}
It calls MessageBox
which is defined inside Windows user32.dll
library. Runtime does all the heavy work for you here, although sometimes you'd need to manually manage memory. If you get the signature wrong, your program may fail on the call, you may introduce a leak or the method might return something completely different, so be careful! I find pinvoke.net a great instrument to correct signatures for different APIs.
Some extern
methods inside .NET Framework that don't have have [DllImport]
attribute but are decorated with [MethodImpl (MethodImplOptions.InternalCall)]
attribute are usually the ones implemented in CLR itself, which is written in C as well. Some of such methods just can't be implemented in C# because they manage runtime itself, and some are implemented in C because their performance is critical and C is faster.
This is what MSDN says about them:
Specifies an internal call. An internal call is a call to a method that is implemented within the common language runtime itself.
As for looking at the actual implementation code, I doubt you'll be able to get it from Microsoft but there are some cool alternative implementations of CLR around so be sure to check them out.

- 18,612
- 4
- 58
- 83

- 264,556
- 84
- 409
- 511
-
1One thing to keep in mind, often when working with extern and DLLImport you will often find references to "unsafe" as well. Keep in mind this is allowing you to use such things as actual pointers and when writing your own extern methods you must be very careful to not incur any memory leaks. – VulgarBinary Feb 24 '11 at 21:44
We use " extern " modifier in method declaration. It is used to indicate that the method is implemented externally. A common use of the " extern " modifier is with the DllImport attribute. Non-C# function calls are managed with this attribute. If you are using extern modifier then you have to include following namespace:
using System.Runtime.InteropServices;
Syntax is somthing like:
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);

- 31
- 4
extern
is with platform invocation (pinvoke) to facilitate managed assemblies calling into unmanaged code. The extern
keyword informs the compiler that it will need to generate the correct code for allow for the correct data marshaling.

- 22,160
- 4
- 52
- 69