I was re-reading through some .Net documentation today when I noticed that the first portion of the extern
keywords documentation claims:
The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code.
What caught my attention was that the document states that "a common use" of extern
is that it is used with the DllImport attribute. This implies that there are other use-cases where DllImport is not required. I've not had to integrate many external, non-managed libraries into my applications but in all cases the linked methods were defined with DllImport.
I've searched multiple queries through Google and MSDN and I can't find a case or explanation of when the extern
keyword would be used without defining the method as an external method import from an unmanaged dll.
How, and when, would you use the extern
keyword without defining the [DllImport(...)]
attribute on the method definition?
Please note, this is not specific to using extern
when defining aliases. That is a different use of the keyword and that case is outlined in a different article within the MSDN C# language reference.