C # Trivia

Source: Internet
Author: User

1. When you create the first instance of a type, the order of operations is:
1. Static variable set to 0
2. Execute static variable initializer
3. Executing a static constructor for a base class
4. Execute static constructors
5. Instance variable set to 0
6. Execute 衯 variable initializer
7. Execute the appropriate instance constructor in the base class
8. Execute instance Constructors

A typical example:

  

 Public classa{ Public Static ReadOnly intx; StaticA () {x= B.y +1; }} classb{ Public Static inty = a.x +1; Static voidMain (string[] args) {Console.WriteLine ("X:{0},y:{1}. ", a.x, y);    Console.ReadLine (); }}
View Code

The default initial value of a global variable for a simple value type is 0. Because the entry function is in class B, the program first loads B, because Y is a static member of B, the value of Y is computed when the class is loaded, but the value of Y is found to be obtained by assigning a value, and the value of x is immediately computed. X is initialized by the static constructor of Class A, and references the y of B, at which point y is still 0 (because the assignment statement of Y is not finished, so its value is the default), get x=0+1=1, continue to be worth to Y y=x+1=2.

For details, refer to C # static constructors (note the comments later in this article)

C # Trivia

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.