12

Accordingly to the section "10.12 Static constructors" of "C# Language Specification. Version 5.0" static constructor can be marked with "extern" modifier and in this case it's said to be an external static constructor.

The ordinary (non-external) static constructors are well known. They are used to initialize static fields and properties.

The external static methods are often used to call native functions via P/Invoke.

And I'm also aware of quite esoteric extern constructors (see also this question). For instance, String class has several such declarations, these constructors are implemented by the runtime.

But are the any real usages of external static constructors? I've searched through the coreclr repo and found nothing. The language specification couldn't give a description to some construct that has never ever been used in the wild. Or could?

My guess: C# has external static constructors just because CLR supports them (in principle).

Community
  • 1
  • 1
Andrew Karpov
  • 431
  • 2
  • 7
  • I think you'll find a very similar chunk of text in every subsection of section 10 from 10.6 (Methods) to 10.13 (Destructors). If nothing else, at least it's *consistent*. – Damien_The_Unbeliever Feb 16 '17 at 07:28
  • 1
    The only thing `extern` does is set the RVA of the method to 0. The runtime is left to figure out the rest. See also http://stackoverflow.com/q/38015024/ . There's little reason for the language spec to forbid this since the work of the compiler is trivial (it would be more work to outlaw it). Furthermore, since constructors can't be marked `DllImport`, the only way to implement such methods is with `MethodImplOptions.InternalCall`, so the runtime is the only possible consumer, making it even less interesting to explicitly forbid it. – Jeroen Mostert Feb 16 '17 at 12:09
  • As to why they're not actually used, that goes in the realm of speculation, but if I was a CLR implementer, I'd strongly prefer doing my static initialization in easy to predict spots, rather than relying on the rather arcane rules for initialization of static types. All of that code calls back into the runtime anyway, so it would do nothing but make my job harder. – Jeroen Mostert Feb 16 '17 at 12:12
  • This question looks [http://stackoverflow.com/questions/2385589/purpose-of-c-sharp-constructor-extern-modifier](http://stackoverflow.com/questions/2385589/purpose-of-c-sharp-constructor-extern-modifier) – Hans Mar 09 '17 at 10:23
  • 1
    @JacobKrall That's not a duplicate. The question you referred is about *extern* constructors. I explicitly mentioned that I'm aware of their purpose. But my question is about *extern static* constructors. I haven't found *any* usage of them, that's why I asked the question. – Andrew Karpov Mar 21 '17 at 14:26
  • @AndrewKarpov: retracted; sorry for the confusion! – Jacob Krall Mar 21 '17 at 14:26

1 Answers1

1

From MSDN:

When a constructor declaration includes an extern modifier, the constructor is said to be an external constructor. Because an external constructor declaration provides no actual implementation, its constructor-body consists of a semicolon.

...

It seems we can't think of a good reason of using this declaration and it sure correct. But when you dig further you realize there's a whole world of dynamic assembling or - Code Generation.

If you were to develop a compiler for the .NET platform you'll probably need some trickery solutions as does the C# compiler use. I can realize that some core implementations uses extern constructor which is implemented somewhere else for good design reasons.

Community
  • 1
  • 1
Shahar G.
  • 1,440
  • 12
  • 22