C # use static constructor,

Source: Internet
Author: User

C # use static constructor,

When we want to initialize some static variables, we need to use static constructors. This static constructor belongs to a class instead of an instance. That is to say, this constructor will only be executed once, that is, before the first instance is created or any static member is referenced. NET Automatic Call.

Now we encounter a scenario where a static method is provided, which is used in different places and involves different parameter values, while other contents are completely consistent. If you copy the content of the static method into another method, the code is too redundant and the practice is not desirable. If you use a static constructor to process static variables, You can simplify the code as much as possible. Example:

/// <Summary> /// base class /// </summary> public class A {public static string Field = "original-test "; // static constructor static A () {Field = "test-a";} public static void Test () {Console. writeLine ("the output is:" + Field) ;}/// <summary >/// subclass /// </summary> public class B: A {// static constructor static B () {Field = "test-B";} public static new void Test () {. test (); // call the base class method. This overwrites the base class Test function. Otherwise, call B. test () is actually called Use the Test method of the base class.
} }

As shown in the code, type B inherits from base type. The static constructor in B assigns values to the Field of the static variable, which is called before the Test method in the subclass is called, the implementation of the Test method in B is to completely call the Test method of the base class A. In this way, when the method is executed, the Field value of the static variable used in the method is the test-B value assigned to the static constructor of B.

The call result is as follows:

Static void Main (string [] args) {. test (); // output the output is: test-a B. test (); // output the output is: test-B Console. readLine ();}

A. Test () is called. The Field value is test-a, and B. Test () is called. The Field value is test-B.

In this way, when the implementation logic of the static method is complex, you can simplify the code when you need to personalize the implementation of this method: The subclass reassigns a value to the static variable in the static constructor, then implement the static method in the base class again.

(Note: it is necessary to re-implement the static method in the base class. Otherwise, B is output. in Test (), the Test () method of the base class is called, and the Field variable used is the variable in the base class. At that time, the output is changed to the following :)

Static void Main (string [] args) {. test (); // output the output is: test-a B. test (); // output the output is: test-. Because the subclass method is not overwritten, it is equivalent to A. Test () 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.