C # feasibility of two or more Main () methods in the console or application,
Most Junior programmers or students think that there is only one Main () method in the C # console or application. But in fact there can be multiple Main () methods.
In the C # console or application, a maximum of one Main () method can exist in multiple classes.
1 using System; 2 3 namespace ConsoleTest 4 {5 class Demo 6 {7 public static void Main () 8 {9 Console. WriteLine ("I am the Main method of the Demo class! "); 10} 11} 12 13 class DemoClass14 {15 public static void Main () 16 {17 Console. WriteLine (" I am the Main method of the DemoClass class! "); 18 Demo. Main (); 19 Console. ReadLine (); 20} 21} 22}
As shown in the above Code. The Demo class and DemoClass class each have a Main () method. Then we compile it.
, Error message, failed to compile. The Main reason is that the program written in the preceding method has two Main () entries. Therefore, an error is prompted.
However, we can use the program settings in VS to compile successfully. Open project properties, as shown in.
Change the default setting of "Startup object" in the project property to ConsoleTest. DemoClass, save it, and compile it again. Such as the running result.