static constructor RRS feed

  • Question

  • A static constructor will be called even when there is no reference of the class.
    Tuesday, October 8, 2019 5:40 PM

All replies

  • You can't rely on it. The specification only says that the static constructor is guaranteed to be called before any calls are made to the class. So if the class is not used, then the static constructor might not be called.

    In fact, I just tried it out in a sample application and indeed the static constructor was not called unless I used the class.

    • Proposed as answer by CoolDadTx Wednesday, October 9, 2019 1:39 PM
    Tuesday, October 8, 2019 6:11 PM
  • Hi him_sharma,

    Thank you for posting here.

    According to your description, it seems that you want to know more about static constructor. You could refer to the MSDN doc.

    Static Constructors (C# Programming Guide)

    Hope his could help you.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 9, 2019 1:25 AM
  • This is true up to .NET v4.0.

    There is optimization that from .NET v4.5 onwards, except certain lists of hardcoded classes like String, if your static members are not called, the static constructor will not be called.

    Therefore on .NET v4.5 onwards, in the case of adding reference to libraries with classes that you don't intent to use but have heavy duty static constructors, you no longer need to burden the cost of initializing these classes.
    Wednesday, October 9, 2019 3:35 AM