Static code blocks

前端 未结 5 2289
遇见更好的自我
遇见更好的自我 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:42

    -A static constructor doesn't have any parameter.
    -A static class can contain only one static constructor.
    -A static constructor executes first when we run the program.

    Example:

    namespace InterviewPreparation  
    {  
        public static class Program  
        {  //static Class
            static Program()  
            { //Static constructor
                Console.WriteLine("This is static consturctor.");  
            }  
            public static void Main()  
            { //static main method
                Console.WriteLine("This is main function.");  
                Console.ReadKey();  
            }  
        }  
    }  
    

    Output:
    This is static constructor.
    This is main function.

提交回复
热议问题