I. Stack and stack
InProgramDuring runtime, its data is stored in the memory. WhileTypeDetermines how much memory a data item needs, where it is stored, and how it is stored. The running program uses two memory areas to store data-stack and heap.
1. Stack
1). Definition
Stack is a memory array, which is a data structure of LIFO (last-in first-out. The size of the stack space required by the program during compilation has been allocated. Stack is generally used to store variable values, the execution environment of the current program, and parameters for passing methods.
2). Features
Data can only be inserted and deleted from the top of the stack. inserting data into the top of the stack is called "push". deleting data from the top of the stack is called "pop ).
2. Heap
A heap is a memory area. In the heap, a large block of memory can be allocated to store certain types of data objects. Unlike stack access, the memory in the stack can be stored and removed in any order.
Ii. Variables
1. variable definition
A variable is the name of the program for the memory space. It indicates the data stored in the memory during program execution. Variables can be divided into the following four types based on their locations:
1). Local variables: variables defined in the method, used to save temporary data in the method scope.
2). Parameter: a temporary variable defined in the parameter list of a method. It is used to pass parameters from one method to another.
3). Field: As a type member, it is defined in the type to save data related to the type or type of instance.
4). array element: defined in an array as a member of an ordered set of data items.
2. Variable declaration and initialization
The declaration of variables completes two tasks:
1) Name the variable and specify a type for it.
2) Let the compiler allocate a piece of memory space for it.
Variable declaration and initialization are often completed using a statement. Declare a variable and assign an initial value to the variable. I will write them separately here to illustrate the entire process.
1//1. Declare an integer variable X2IntX;3//2. initialize x4X =10;5//3. Use Variables6Console. writeline (X );
3. multi-variable Declaration
You can declare multiple variables in a statement. However, the following rules must be observed:
1). The types of multiple variables must be the same and can only be written once at the beginning of the statement.
2) multiple variable names are separated by commas.
3). Each variable name can be followed by an initialization statement.
1//Declare three integer variables X, Y, and Z with one statement.2IntX, Y =10, Z =1000;
4. Automatic initialization of Variables
Some types of variables are automatically set to the default value of this type if they are not initialized during Declaration; the value of other variables that cannot be automatically initialized as the default value is "undefined" before being assigned a value ".
Parameters of local variables and methods cannot be automatically initialized, while fields and array elements of classes can be automatically initialized.