How does a static constructor work?

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


        
10条回答
  •  离开以前
    2020-12-04 06:51

    According to the MSDN, a static constructor:

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

    So the static constructor will be called before the static method MyClass.MyMethod() is invoked (assuming not also invoked during static construction or static field initialization of course).

    Now, if you are doing anything asynchronous in that static constructor, then it's your job to synchronize that.

提交回复
热议问题