C # static Dry Goods Full analysis

Source: Internet
Author: User

Order of Instruction
  • Background

  • Static fields

  • static functions

  • Static methods

  • Questions and Answers

  • background
      • Static source

    When writing a class, it is sometimes necessary for a member of a class to be unique, that is, to keep only one state for all objects. such as the creation of personal information, we are Chinese, but it is not always everyone to save a Chinese field, with one is enough .

      • Static effect

    MSDN says: Using the static modifier declares a static member that belongs to the type itself rather than to a particular object. In other words, the object modified with static is all members of a class and is not unique to a class. For example, a sedan has four wheels, not a car with four wheels.

      • Static properties

    1. Access does not require declaring class objects. Like what

    Console.Write (); // Write () This is a static method and does not require new Consolo ();

    2, the static object is unique, regardless of whether it produces an object or, regardless of the number of objects produced, some specific data in memory space only one copy. In other words, you have to change the number of static variables, no matter how many members you have in the new class.

    Static fields

    The so-called static field, which is the field with static modification (yes, that's nonsense O (∩_∩) o ).

      • Role

    No matter how many objects are created, static fields represent only one of the data in memory space. For example, the system time, no matter how many software you installed, you want to know what time it is, there is only one reference value, if you change the time, then the other software acquisition time has changed. (Of course, just for example, specifically, I don't care about the time of visit).

      • Sequence of operations

    A static field is the first object that is instantiated when a class operation (instantiation, calling a static method, and so on) is initialized. The second is a static constructor, and both of them can only be called once.

      • Characteristics

    1. Because the static member itself is for global uniqueness, of course, static members within the entire function equivalent to global variables.
    2. A static member can only be initialized once, and is created the first time the class is manipulated. When other operations are performed on the entire class, it is no longer created because the static members are already there. (That is, to the rookie of the nonsense, or many people will have doubts, such as me.) )
      • Example

    • For example, each time an instance is created, the constructor makes +1 for two fields, but the result is different, which is the uniqueness of the static modified field

    Static Constructors
      • Role

    Static constructors are typically used to initialize static variables and do not initialize non-static variables, but because non-static constructors can also manipulate static variables, it is less useful to use static constructors.

      • Sequence of operations

    Regardless of what you do with a class, a static constructor is always the first method to be manipulated except for a static field, which is more advanced than a non-static parameter, a non-static constructor.

      • Characteristics
    1. Only static members can be manipulated, because non-static members require an entity reference.
    2. The entire lifecycle is run only once and cannot be called.
    3. A static constructor cannot have modifiers, because only the system can be called, and there is no public that is useless; it cannot have input parameters.
    4. If the class contains the main method, the static constructor is executed first;

      • Example
    classA { Public Static intX; StaticA () {X= B.y +1; }    }    classB { Public Static intY = a.x +1; StaticB () {}Static voidMain (string[] args) {Console.WriteLine (string. Format ("y={0}, X={1}", B.y, a.x));//y=2, x=1;Console.ReadLine (); }    }

    This is a very famous online example of static, if you do not look at the answer, can be counted out is the ability.

    1. First, the Main () method is inside Class B, so you need to call Class B first.

    2. Enter Class B, assign space to the static field y first, and assign a value of 0 by default; Notice that Y is initialized here.

    3. Because y=a.x+1, the static field of Class A is now entered in Class A.

    4. In the same way, a class X is assigned a space and assigned a value of 0. Then x=b.y+1,b.y already exists, for 0, so x=b.y+1 after execution, X = 1.

    5. Call the static constructor of a.

    6. Complete the assignment of Y, y=a.x + 1,y=2.

    7. Call the static constructor of B.

    8. The main () method is called, because A.X,B.Y is initialized and assigned a value, so write the answer directly.

    To be able to describe the complete sequence, static general interview questions are hard for you (I mean I am very good)

    Static Methods
      • Application

    Static methods are generally used for frequent calls and infrequently modified places. such as the basic statement to connect to the database. Of course, this feature is also due to the fact that static methods cannot be inherited and cannot be extended.

      • Sequence of operations

    Static methods can be called after static fields and static constructors are manipulated.

      • Characteristics

    1. A static method call does not require instantiating a class.
    2. Static methods can only reference static methods, fields, properties. Static methods cannot have non-static variables.

    3. Once a static method is called, it cannot be destroyed unless the entire application domain is finished. So the static function cannot be too much, otherwise it always occupies memory.

    4. Because static methods cannot access non-static variables, they are less coupled with non-static methods, which facilitates code modification.

    5. Static methods are more efficient than non-static methods, but cannot be used frequently because they do not always consume memory and cannot be destroyed.

    Questions and Answers

  • What is the biggest difference between a static variable and a non-static variable?

    The biggest difference is in the location of the memory. The memory of a static variable takes up memory when the program starts to hold the variable, and the variable frees the memory until the end of the program. A non-static variable allocates memory when the program runs to that step and automatically destroys it when it finishes executing. Therefore: The value of the static variable is initialized only once, and each subsequent access is the last processed value.

  • Why static variables do not need to be instantiated to be accessible.

    Since classes are initialized, they have allocated memory space for static variables, they can be found exactly where they are without instantiation, and non-static members must instantiate the class object before they can determine their memory location; So there is also the argument that a static member belongs to a class and not a static member belongs to the object of the class.

    Why can't I access non-static variables inside a static method, or create non-static variables?

  • Because a non-static variable needs to be instantiated to confirm its location in memory, the static function does not need to be instantiated, so the static method cannot use the

  • Why can't I declare a static variable inside a method?

    Because a static variable is a common variable for all instances of a class, it is a global variable within the scope of the class. If you define a static variable inside a method, it causes the static variable that is defined to be a local variable, and so the definition naturally goes wrong.

  • The life cycle of a static member

    The life cycle of a static member is the lifetime of the entire application domain. That is, start with the class that contains the static member, until the end of the entire application.

  • The
    above information is my reference to the countless materials themselves, of course, there may be some errors, welcome to correct my aunt, I will try to revise, let this article as a good static reference.

    C # static Dry Goods Full analysis

    Related Article

    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.