Static code blocks

前端 未结 5 2111
遇见更好的自我
遇见更好的自我 2021-02-06 19:56

Going from Java to C# I have the following question: In java I could do the following:

public class Application {
    static int attrib         


        
5条回答
  •  北海茫月
    2021-02-06 20:54

    public class Application
    {     
    
        static int attribute;     
        static Application()
        {         
             attribute = 5;     
        }    // removed
    }
    

    You can use the C# equivalent static constructors. Please don't confuse it with a regular constructor. A regular constructor doesn't have a static modifier in front of it.

    I am assuming your //... rest of the code need to be also run once. If you don't have such code you can just simply do this.

     public class Application
     {     
    
        static int attribute = 5;
     }
    

提交回复
热议问题