A typical example and code for learning C # static functions and variables

Source: Internet
Author: User

(1) It is used to initialize static and read-only fields.
(2) You cannot add access modifiers when adding static keywords because static constructors are private.
(3) Static constructor of a class can be executed at most once in a given application domain: only the static constructor of the class is activated when an instance of the class is created or any static member of the referenced class is activated.
(4) Static constructors cannot be inherited and cannot be called directly.
(5) If the class contains the Main method used to start execution, the static constructor of the class will be executed before calling the Main method. For any static field with an initial value, the initial values must be executed in the text order before executing the static constructor of this class.
(6) If a static constructor is not compiled and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.
The following example code is used for further explanation: Copy codeThe Code is as follows: /*************************************** ***********
* Static structure function training
* (1) ① ③ ...... Execution Order
* (2) output result: static ()
* Static B ()
* X = 1, Y = 2
**************************************** ***********/
Using System;
Class
{
Public static int X;
Static A () // ④ return to ③ After execution
{
X = B .Y + 1;
Console. WriteLine ("static ()");
}
}
Class B
{
Public static int Y = A.X + 1; // ③ call the static member of,
// Go to the static constructor of A ---->
Static B () // ② If the static field with the Initial Value Setting item,
// When executing the static constructor of this class,
// Execute the initial values in the text order first.
// Go to the initial value setting item ---->
{
Console. WriteLine ("static B ()");
}
Static void Main () // ① program entry,
// If the class contains the Main method used to start execution,
// The static constructor of this class will be executed before the Main method is called.
// Go to the static constructor of B ---->
{
Console. WriteLine ("X = {0}, Y = {1}", A.X, B .Y); // ⑤ output result
Console. ReadLine ();
}
}

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.