SoFunction
Updated on 2025-03-01

Example of usage of static static variables in C#

This article describes the usage of static static variables in C#. Share it for your reference. The details are as follows:

Use the static modifier to declare a static member belonging to the type itself rather than to a specific object static modifier can be used for classes, fields, methods, properties, operators, events, and constructors, but not for types other than indexers, destructors, or classes.
 
Static global variables

Definition: Before the global variable, add the keyword static. The variable is defined as a static global variable.

Features:
① , This variable allocates memory in the global data area.
②. Initialization: If it is not explicitly initialized, it will be implicitly initialized to 0.
 
Static local variables

Definition: When the static keyword is added before the local variable, the static local variable is defined.
Features:
① , This variable allocates memory in the global data area.
②. Initialization: If it is not explicitly initialized, it will be implicitly initialized to 0.
③ , It always resides in the global data area until the program runs. However, its scope is a local scope. When the function or statement block that defines it ends, its scope ends.

Static data members

Features:
① , Memory allocation: allocated in the global data area of ​​the program.
②, Initialization and definition:
a. Space must be allocated when defining static data members, so they cannot be defined in the class declaration.
b. In order to avoid repeatedly defining the source files using this class, the location cannot be in the header files of the class.
definition.
c. Static data members must exist since the program is run at the beginning, so the optimal location for their initialization is implemented internally in the class.
③, Features
a. The impact on the public, protected, private keyword is the same as that of ordinary data members.
b. Because its space is allocated in the global data area and is shared by all objects of this class, it does not belong to a specific class object. Its scope is visible when the class object is not generated, that is, when there is no instance of the class, we can operate it.

④, Access form
a. Class object name. Static data member name
⑤. Static data members are mainly used on properties owned by all instances of the class. For example, for a deposit class, the account is different from 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 benefits. First, no matter how many deposit-class objects are defined, the interest data members share the memory allocated in the global area, so they save storage space. Second, once the interest needs to be changed, as long as it is changed once, the interest of all deposit-type objects will be completely changed, because they actually share the same thing.

Static member functions

Features:
① . Static member functions are associated with classes, not with class objects.
②. Static member functions cannot access non-static data members. The reason is simple, non-static data members belong to specific class instances.

Function: 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:

Copy the codeThe code is as follows:
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)
        {
            ("i={0}",i);
            ("j={0}", new class1().j);
            ();
        }
    }
}

Now analyze the above code:

Copy the codeThe code is as follows:
(("i={0}",i));
Here i is a static variable, and class class1 is the first time it is cited. You must first allocate memory for all static variables in class1. Although there is now hyperthreading technology, instructions are still executed in logic one by one in order, so first allocate memory for static int i, and keep the default value of int in that memory 0, and then allocate memory for static int num variable, and of course the value is 0.

Then perform the second step and assign values ​​to the variable: first assign values ​​to the static int i variable, i=getNum(), look at the code in getNum, which is return num. At this time, the value of num is 0, so i is 0. Then assign a value to the variable num, num=1; after this line of code is executed, num will be 1.
So the final result is:
i=0 j=1

I hope this article will be helpful to everyone's C# programming.