What is the purpose of external static constructors in C#?
Asked Answered
C

1

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).

Criticize answered 15/2, 2017 at 19:55 Comment(6)
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.Adiell
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 https://mcmap.net/q/873252/-when-is-it-appropriate-to-use-the-extern-keyword-without-using-the-dllimport-attribute . 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.Spagyric
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.Spagyric
This question looks https://mcmap.net/q/882385/-purpose-of-c-constructor-extern-modifierAgony
@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.Criticize
@AndrewKarpov: retracted; sorry for the confusion!Accused
R
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.

Rubella answered 20/3, 2017 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.