How does a static constructor work?

前端 未结 10 1308
迷失自我
迷失自我 2020-12-04 06:03
namespace MyNameSpace
{
    static class MyClass
    {
        static MyClass()
        {
            //Authentication process.. User needs to enter password
                


        
相关标签:
10条回答
  • 2020-12-04 07:06

    From the documentation (emphasis mine):

    A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

    0 讨论(0)
  • 2020-12-04 07:06

    Or you could step through in the debugger.

    0 讨论(0)
  • 2020-12-04 07:09

    Here's the actual order in which things go down:

    1. Start of Main
    2. Start of the static MyClass constructor
    3. End of the static MyClass constructor
    4. Start of MyMethod
    5. End of Main
    0 讨论(0)
  • 2020-12-04 07:10

    The static constructor will be called before mymethod is executed. However if you are screwed if 4 is called before 2 then I suggest you re-think your design. Should not be doing complicated stuff in a static constructor anyway.

    0 讨论(0)
提交回复
热议问题