C# entry point function

前端 未结 6 621
闹比i
闹比i 2020-11-29 12:55

Is staic void main() is necessary for entry point function in C# or we can use some other functions? Why main() is static?

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 13:52

    The Main method might be what you consider as the entry point of an application but in c# as far as i know methods can't be defined directly in namespaces which means it has to be within a class. The real first executed thing then is the static constructor of the class containing the Main method

    using System;
    namespace test
    {
        class Program
        {
            static Program()
            {
                Console.WriteLine("static constructor");
            }
    
            public static void Main(string[] args)
            {
                Console.WriteLine("Main method");
            }
        }
    }
    

    Outputs static constructor first and then Main method

提交回复
热议问题