An example of the use of static static variables in C

  • 2020-11-26 18:58:05
  • OfStack

This article illustrates the use of static static variables in C#. Share to everybody for everybody reference. The details are as follows:

The static modifier is used to declare static members that belong to the type itself rather than to a particular object. The static modifier can be used on classes, fields, methods, properties, operators, events, and constructors, but it cannot be used on types other than indexers, destructors, or classes

Static global variable

Definition: The global variable is defined as a static global variable by adding the keyword static before it.

Features:
This variable allocates memory in the global data area.
, initialization: if it is not initialized explicitly, it will be initialized implicitly to 0.

Static local variable

Definition: A static local variable is defined when the static keyword is appended to the local variable.
Features:
This variable allocates memory in the global data area.
, initialization: if it is not initialized explicitly, it will be initialized implicitly to 0.
, it always resides in the global data area, until the end of the program run. But its scope is local, ending when the function or block defining it ends.

Static data member

Features:
Memory allocation: allocation in the global data area of the program.
Ii. Initialization and definition:
Static data member definitions allocate space and cannot be defined in class declarations.
To avoid duplication of definitions in multiple source files that use the class, it cannot be in the header file of the class
Definition.
Static data members must exist as soon as program 1 starts running, so the best place to initialize them is within the class.
(3) characteristics,
The influence of the public,protected,private keyword it and ordinary data members 1,
b, because its space is allocated in the global data area and belongs to all objects of this class, so it does not belong to the specific class object, its scope is visible when no class object is generated, that is, when no instance of the class is generated, we can operate on it.

, the form of visit
a, class object name. Static data member names
(5) Static data members, mainly used in all instances of the class have the property. For example, for a deposit class, the account number is different for each instance, but the interest rate for each instance is the same. Therefore, interest should be set as a static data member of the deposit class. This has two advantages. First, no matter how many deposit class objects are defined, interest data members share memory allocated in the global area, thus saving storage space. 2. Once the interest has to be changed once, the interest on all the deposits has to be changed, because they're essentially sharing one thing.

Static member function

Features:
Static member functions are associated with the class, not with the object of the class.
(2) Static member functions cannot access non-static data members. The reason is simple: a non-static data member belongs to a particular class instance.

Effect: Mainly used for operations on static data members.
Call form:
, class object name. Static member function name ()

Examples and analysis of static static variables
Example:

using System;
namespace teststatic
{
    class class1
    {
        static int i = getNum();
        int j = getNum();
        static int num = 1;
        static int getNum()
        {
            return num;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("i={0}",i);
            Console.WriteLine("j={0}", new class1().j);
            Console.Read();
        }
    }
}

Now analyze the above code:

Console.WriteLine(string.Format("i={0}",i));
Here, i is the static variable, and class class1 is cited for the first time. Memory is allocated for all static variables in class1. In spite of the hyper-threading technology, instructions are executed in logical sequence one by one, so first allocate memory for static int i and keep the default value of int 0 in that memory, and then allocate memory for the variable static int num, which of course also has a value of 0.

Then perform step 2 to assign the variable: first assign the variable static int i (), i = getNum(), look at the code in getNum, return num, at this time num value is 0, so i is 0. Then assign the variable num, num = 1; After this line of code is executed, num is 1.
So the final result is:
i = 0 j = 1

Hopefully this article has helped you with your C# programming.


Related articles: