C # class, static class, static field, static method

Source: Internet
Author: User

This week's research covers various types of classes and application scenarios. Static class static methods, understanding of static classes, and application scenarios.

One point in the book is as follows:

1 whether the type is static or not, it can contain static methods and static fields. It is best to have a static constructor to initialize static fields in the type?

2. Understanding of abstract classes: it is not a specific concept, but a common concept. It should be similar to the concept of structure. For example, the trapezoid shape of a circular triple row is a shape, but what is the shape, then you can define an abstract class of the shape.

3. Static class, static method, and static field should be placed in the stack, and the address is determined. Therefore, when the instance is not created, the field is determined. Instead of static classes, static methods are divided. Non-static fields are placed in the heap. Only when programmers create their instances, the address is determined and can be accessed.

4. Do not abuse static classes and static methods. Too many process-oriented programs are written under the OO programming banner.

Why should I use static fields and static methods in the class. Why should I put static fields, methods, non-static fields, and methods in a class together? This is a question .?

I know that one application scenario is the singleton mode? Are there other application scenarios?

Summary of static methods and application scenarios of static fields:

A singleton Mode

B and the type of the instance.

C production work

D

The code for learning static methods is as follows:

Using system; using system. data; using system. text; namespace mystu {/* learn static fields, static methods, and single-instance mode implementation */class programe {static void main (string [] Arg) {try {baseclass. getinstancecount (); baseclass OBJ = new baseclass (); obj. test (); baseclass. getinstancecount ();} catch (exception ERR) {console. writeline (err. message) ;}} sealed class baseclass {Private Static int instancecount = 0; public static void getinstancecount () {console. writeline ("the number of instances of the current class is" + instancecount. tostring ();} public void test () {instancecount = instancecount + 1; // Add 1 Console to the number of instances. writeline ("create instance ");}}}

C # implementation code of Singleton Mode

Using system; using system. data; using system. text; namespace mystu {/* learn static fields, static methods, and single-instance mode implementation */class programe {static void main (string [] Arg) {try {baseclass. getinstancecount (); // This is incorrect because the constructor is private and cannot create instances. You can only create baseclass obj3 = new baseclass () in singleton mode (); // obj. test (); baseclass OBJ = baseclass. createinstance (); baseclass. getinstancecount (); baseclass obj1 = baseclass. createinstance (); baseclass. getin Stancecount ();} catch (exception ERR) {console. writeline (err. message) ;}} sealed class baseclass {static baseclass OBJ = NULL; static int instancecount = 0; public static void getinstancecount () {console. writeline ("the number of instances of the current class is" + instancecount. tostring ();} baseclass () {instancecount = instancecount + 1; // Add 1 Console to the number of instances. writeline ("create instance");} public static baseclass createinstance () {If (OBJ = NULL ){ OBJ = new baseclass (); // is there a question here, so that the constructor is not called during instance creation? After verification, the constructor is also called here ?} Return OBJ ;}}}

Running result of Singleton Mode

---------- Run ---------- ==================== run C # program write by senjin technology ==================== = the number of instances of the current class is 0. The number of instances of the current class is 1. The number of instances of the current class is 1 ================= Program end copy cicnor 2001 ==================== output completed (0 sec consumed) -Normal termination

In addition, I want to tell you the secret that all my test code is compiled and compiled in editpllus, which has many advantages,

A is out of the compiler, and all the code is written by myself. Many keywords are remembered.

B. Because everything is written by memory, the understanding of a lot of knowledge is deepened,

All that is, if you want to learn, you can use editplus to write code. If you want to work, you can use vs. The smart prompting function can indeed accelerate development efficiency.

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.