none
static constructor RRS feed

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.

    Tuesday, October 8, 2019 6:11 PM
    Moderator
  • 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,

    Jack


    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
    Moderator
  • 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
    Answerer