In the previous article "Delegate (Delegate) Type", I used the static keyword when defining the public method, which resulted in the inability to access this method through the form of object.method name. In this article, we will learn more about static static variables. .
Variables:
Before learning static static variables, we still need to understand the meaning of variables. Programs need to perform operations such as reading, writing, and computing data. When a specific value or result needs to be saved, variables need to be used. From the user's perspective, a variable is the name used to describe a piece of information. Each variable can be stored in a variable. Various types of information, such as: a person's name, the price of a ticket, etc.; from the computer's perspective, the variable represents the storage address, what type the variable is, and what type of value is stored in the variable. An important principle when using variables is that variables must be defined first and then used.
The definition and usage rules of variables in C# are similar to those in C/C++, so I won’t go into details here (so it is quite important to learn C well^_^)
static static variable:
Variables declared with the static modifier are called static variables. Once the class to which the static variable belongs is loaded, it will exist until the end of the program containing the class. There are two main properties of static:
1. Hidden:
Static methods or static variables defined in a class belong to the class itself, not to an object of that class. To call a method defined as static, it must be preceded by the name of the class. (Even public access modification will not work, which is why at the end of the previous article) Instance methods must be used through instances of the class. Instance methods can use non-static members of the class as well as static members of the class.
Access rules:
Static methods can only access static members of the class and cannot access non-static members of the class;
Non-static methods can access static members of the class and can also access the class Non-static members;
Static methods cannot be called using instances, but can only be called using class names.
For example, the following example:
class person { public static int i; //定义一个静态变量i 默认值是0 public int k=0; //定义一个非静态变量 k; public static int sbu() // 定义一个静态方法 { i = 9; //成功对静态变量赋值 k = 5; //出错了,无法访问非静态变量 return k; //综上静态方法只能访问静态变量 } public int Add() //定义一个实例方法 { i = 9; //对静态变量赋值没有问题 k = 5; //对非静态变量赋值也没问题 return i; //综上实例方法能够访问所有类型变量 } }
We instantiate a person and an object to access the method:
static void Main(string[] args) { person per = new person(); //实例化一个对象per int i = per.i; //出错了,per访问不了类里的静态变量 int k = per.k; //没有问题 per.sbu(); //出错了,per访问不了静态方法 person.sbu(); //成功调用 per.Add(); //成功调用 person.Add(); //出错了,person访问不了实例方法 }
2. Keep the variable content persistent:
The variables stored in the static data area will be initialized when the program starts, and it is also the only initialization. There are two types of variables stored in the static storage area: global variables and static variables. However, compared with global variables, static can control the visible range of variables. After all, static is used to hide.
Just write an example and you will know (this time I wrote it in C++) C# does not allow the use of static in methods:
#include<iostream> using namespace std; int main() { for (int i=0;i<4;i++) { static int k =0; //定义一个静态变量并赋值为0 k++; cout<<k<<endl; //输出 } return 0; }
Result:
If we remove static in the above code, then k=0; becomes a non-static variable, and the result will only be a number 1;
Constant:
A constant is a quantity whose quality is fixed. From the perspective of data type, the type of a constant can be any value type or reference type. The declaration of a constant is to declare the constant name and its value to be used in the program. (Usage is also similar to C) But in C#, once a constant is defined, its value cannot be changed
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace demo { class Program { const int S = 9; // 定义一个常量S并赋值 static void Main(string[] args) { S += 4; //出错了,常量一旦定义就不能改变常量的值 Console.WriteLine(S); } } }
The above is the content of C# Learning Diary 20----static static variables and constants, more related content Please pay attention to the PHP Chinese website (www.php.cn)!