C #12-12-Why must the mian function be static?

Source: Internet
Author: User
C # Program At the beginning of the startup, there will be a unique entry function main (), rather than a static member, also known as an instance Member, which must act on the instance. When the program starts running, no instances are created, so instance members, including non-static main methods, cannot be called. To be able to execute the main method at the beginning of the program, it must be declared as static.
By the way, the members called in the main method must also be static unless an instance is created.
For example:
 Namespace Lover_p.test {
Public Class Test {
Public Void Instancemethod (){} // Instance Member (non-static)
Public Static Void Staticmethod {} // Type member (static)
Public Static Void Main (){
Instancemethod (); // Error! The instance Member is called, but no instance is created at this time.
Staticmethod (); // Correct! You can call static members.
Test sometest =New Test (); // Create an instance of this type
Sometest. instancemethod (); // You can call the instance members on this instance.
Sometest. staticmethod (); // Additionally, it is also incorrect to call static members on an instance!
}
}
}

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.