The difference between C # static variable and ordinary variable

Source: Internet
Author: User

the difference between static variable and ordinary variable


The description of the global variable (external variable) before being labeled static constitutes a static global variable. The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are not different in the way they are stored. The difference between the two is that the scope of the Non-static global variable is the entire source program, and when a source program consists of multiple source files, non-static Global variables are valid in each source file. A static global variable restricts its scope, that is, it is only valid within the source file that defines the variable, and it cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can only be common to functions within that source file, so it is possible to avoid causing errors in other source files.


Static global variables and ordinary global variables
The static global variable is only initialized once, and the scope is limited to the source file of the variable, preventing it from being referenced in other file cells

Static local variables and ordinary local variables
The static local variable is initialized only once, and the next time based on the last result value

static function and ordinary function
The static function has only one copy in memory, and the normal function maintains a copy of each call

initialization order of static

Let's take a look at an example:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ConsoleApplication1
{
    class B
    {public
        static int Y = a.x + 1;

        Static B () {}

        static void Main (string[] args)
        {
            Console.WriteLine ("X={0},y={1}", a.x, b.y);
            Console.read ();
        }


    public class A
    {public
        static int X;
        Static A ()
        {
            X = b.y + 1;
        }
}}

There is a picture of the truth:



Execution result is x=1,y=2

This example mainly examines 2 aspects, one is the static usage, the other is the static initialization order. Knowing the initialization order and rules of static, the answer to the question is easy to understand. Here are three types of static objects: static members, static methods, static constructors. The rules are as follows:

The static constructor of a class executes only once in a given application domain. The static constructor is triggered by the first occurrence of the following events in the application domain:

1 The instance of the class is created.

2 any one static member is referenced

3 If the class contains the Execute entry main method, the static constructor of this class executes before the main method is invoked.

4 If the class contains any STAIC members, the static members are initialized before the static constructor.

5 If the class contains any static methods, the static methods are initialized after the static constructor.

6 for multiple static members, their initialization will be in the order of text, and will not change because of the order in which they are called.

Now look at the above application, Class B has a main execution entry, so B is initialized first, in the order of the static member Y->static constructor. When y is initialized, the a.x is referenced, and the compiler begins initializing class A (Note that Class B initialization is not complete), and the order is also a static member X->static constructor. In Class A, X is not given an initial value when it is defined (when defining the static variable as much as possible to give the initial value), the compiler defaults to the value 0 (int type). then the static constructor is executed, because Class B initialization is not completed at this time, So the value of B.Y is then given a default value of 0 by the compiler, so after the static constructor of a is executed, the value of X becomes 1, and then the B continues to complete initialization, and the value of Y is 2. Finally executes main, outputting the values of a.x and B.Y.


======================================================= 's pathetic programmer ================================================== ============

1. What is the difference between a static global variable and a normal global variable? What is the difference between a static local variable and a normal local variable. What is the difference between a static function and a normal function.
A: The global variable (external variable) before the description of the static is composed of the global variable. The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are not different in the way they are stored. The difference between the two is that the scope of the Non-static global variable is the entire source program, and when a source program consists of multiple source files, non-static Global variables are valid in each source file. A static global variable restricts its scope, that is, it is only valid within the source file that defines the variable, and it cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can only be common to functions within that source file, so it is possible to avoid causing errors in other source files.
From the above analysis, we can see that the change of the local variable to the static variable is the change of its storage mode that changes its lifetime. Changing the global variable to a static variable changes its scope and limits its use.
The static function differs from the normal function scope. Only in this document. Functions that are used only in the current source file should be described as internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, you should indicate in a header file that the source file to use these functions contains this header file
What is the difference between a static global variable and a normal global variable: Static global variables are only first made once, preventing them from being referenced in other file cells;
What is the difference between a static local variable and a normal local variable: the static local variable is only initialized once, and the next time it is based on the previous result value;
What is the difference between a static function and a normal function: There is only one copy of the static function in memory, and the normal function maintains a copy of each call


2.static what is the purpose. (Please specify at least two types)
1 in the function body, a variable declared as static maintains its value unchanged during the invocation of this function.
2 within the module (but outside the function), a variable declared as static can be accessed by functions used within the module, but not by other functions outside the module. It is a local global variable.
3 within the module, a function declared as static can only be invoked by other functions within the module. That is, this function is limited to the local scope of the module in which it is declared to use

======================================================= two Tencent pen test ================================================= ===========



reference to the original:

Http://www.cnblogs.com/Ihaveadream/archive/2008/06/27/1231053.html

Http://www.cnblogs.com/lmjob/archive/2009/07/29/1533820.html

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.