Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does extern work in C#?

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()?

like image 357
smartcaveman Avatar asked Feb 24 '11 21:02

smartcaveman


People also ask

Why do we use extern C?

Using extern "C" lets the compiler know that we want to use C naming and calling conventions. This causes the compiler to sort of entering C mode inside our C++ code. This is needed because C++ compilers mangle the names in their symbol table differently than C compilers and hence behave differently than C compilers.

Is extern necessary in C?

You do not necessarily "need" extern for variables. When C was invented Unix linkers were also written, and they advanced the art in unheralded but clever ways. One contribution was defining all symbols as small "common blocks".

Is extern a keyword in C?

Extern is a keyword in C programming language which is used to declare a global variable that is a variable without any memory assigned to it. It is used to declare variables and functions in header files. Extern can be used access variables across C files.


1 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()?

Go to https://github.com/dotnet/coreclr/tree/master/src/vm

like image 79
Eric Lippert Avatar answered Oct 03 '22 21:10

Eric Lippert