C # feasibility of two or more Main () methods in the console or application,

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.