13

Access modifiers like public, private are not allowed on static constructors in C#. Yet, Visual Studio code analysis has a warning in C# security category that says "CA2121: Static constructors should be private".

Is it possible to make a static constructor non-private? Or is that an error of Visual Studio code analysis?

CLARIFICATION: I'm not trying to make any constructor non-private. So "why?" questions are irrelevant. I'm just curious about the contradiction between two Microsoft tools and want to see if there is anything I don't know about how static constructors are handled.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
  • A quote from the link above: `The user has no control over when the static constructor is called. If a static constructor is not private, it can be called by code other than the system. Depending on the operations that are performed in the constructor, this can cause unexpected behavior.` – Tisho Jul 02 '12 at 11:10
  • 1
    I think, the relevant quote is "This rule is enforced by the C# and Visual Basic .NET compilers." So, no, you cannot make it non-private in C#. – Henrik Jul 02 '12 at 11:18

3 Answers3

9

A C# static constructor is always private. You should never see that warning for C# code, and the warning isn't useful there. Code Analysis is available for other languages too, though, and it's those other languages that may cause you to write classes with non-private static constructors.

  • Then why is it stated as applicable to C#? Do you think that's the error? – Sedat Kapanoglu Jul 02 '12 at 11:20
  • @ssg If you're referring to the "This rule is enforced by the C# and Visual Basic .NET compilers." mentioned in the comment on your question, then no, that means that the compiler already ensures that static constructors are private, meaning this warning should never trigger. –  Jul 02 '12 at 11:22
  • Ok I thought that meant "the code analysis rule" that is being enforced, not the compiler error which is thrown when you try to write `private static X() {}`. Do you think it meant "you can't cause this rule to be triggered on C# or VB"? – Sedat Kapanoglu Jul 02 '12 at 11:26
3

If you read the docs it clearly states

If a static constructor is not private, it can be called by code other than the system. Depending on the operations that are performed in the constructor, this can cause unexpected behavior.

I suppose the more important question would be why would you want to?

James
  • 80,725
  • 18
  • 167
  • 237
  • The thing is, I can't. Compiler throws an error when I try to prepend the static with an access modifier. Maybe on the IL level? – Sedat Kapanoglu Jul 02 '12 at 11:24
  • C# does not allow the cctor to be not private, see Hendriks comment above. – usr Jul 02 '12 at 11:24
  • @ssg I would ask yourself *why* you need it to be something other than `private`. I would safely say your design is probably more likely to be wrong than the C# team missing a trick for the usage of non-private static constructors. – James Jul 02 '12 at 11:28
  • @James: I agree but that's not the point, please see my "clarification" edit in the question. I think hvd's answer is right, I think I misread it. – Sedat Kapanoglu Jul 02 '12 at 11:30
1

Is it possible to make a static constructor not-private?

What would be the sense of it?

A static constructor is called the moment the class is loaded from the linker, before any method executes. As such, a static constructor could not sensible be called outside of the automatic call from the linker, which means there is no sense in it having any access modifier.

Basically you can not reference a class WITHOUT THE STATIC MODIFIER EXECUTING - and that before your code that includes the reference executes.

TomTom
  • 61,059
  • 10
  • 88
  • 148