I. Value Type and reference type
1> the value type and reference type divide the data types we have learned into two parts based on the different types of data in memory (stack) storage structure.
2> Value Type: All numeric types: Long int short byte ulong uint ushort sbyte decimal duoble float char bool enumeration Structure
3> reference type: String, arry (array), Class)
4> whether it is a value type or a value of the reference type, copy the data and assign the copy to the variable. The difference is that the value type copies the real value, the reference type copies the memory address of the variable,
That is, the value type is assigned a copy of the actual value, so the value of one variable changes without affecting the value of another variable. The reference type is passed by the memory address (I .e: all point to one variable at the same time). If the value of one variable is found to change the value of another variable, it also changes.
5> the null value can only be a value of the reference type. If the value of a reference type variable is null, it means that it does not point to any object.
Ii. Constants
1> what is a constant? The data modified by const is called a constant.
2> Syntax: const data type constant name = value;
3> once a constant is declared, its value cannot be changed. (That is, a value must be assigned once declared, because a constant cannot be modified after being declared, and can only be assigned after definition)
4> WE declare a constant. During compiler compilation, we will remove the statement that declares the constant, and then directly replace it with the constant value where the constant value is used. So even if it can be changed, there is no effect, and it makes no sense.
5> when a constant is declared, it must be assigned a value. The value assignment of a constant cannot involve a variable because the value of the variable is determined during running, if yes, it violates the definition of constants. That is, the constant is determined during compilation, and the variable is determined during runtime.
Iii. readonly (read-only)
1> readonly can only be used to modify fields, but not local variables. (Because readonly can only be assigned a value in the constructor, that is, when an object is created. Local variables are destroyed after methods (instance methods) are called, such as declarations and creation,
That is, the time period: After the object is created .)
2> the value of a read-only sub-segment cannot be changed.
3> if we assign an initial value to a field while declaring a field for the class, but when the C # compiler compiles the field, it places the assigned code in the constructor. The code assigned to the field is executed only by the CPU.
4> the value of a read-only field can only be changed in the constructor, but cannot be changed elsewhere.
5> what is the difference between readony and const? Const is the value to be determined during compilation, while readonly can be determined at runtime. (After the compilation is run, the code will first be compiled by the compiler to check the syntax, and then compiled into an assembly before running)
6> when compiling, you can determine the value and use const to determine whether to use readonly at runtime.
Iv. Enumeration
1> Syntax:
Access rhetoric Enum enumeration name: Integer type (specify the enumerated value to correspond to the integer type)
{
Enumeration member
}
2> define enumeration members in enumeration, which are separated by commas (,).
3> the enumerated value can only be of the enumerated type, and cannot contain double quotation marks (double quotation marks are strings) or Int type. Otherwise, a conflict occurs.
4> only enumeration items can be defined in enumeration, and each enumeration item has a corresponding int number, which starts from 0 and increases at a time (default ).
A. We can use the forced conversion syntax to forcibly convert the value of one enumerated type variable to its corresponding int type value.
Example:
Direction dir = direction. North; (direction is an enumeration type)
Int I = (INT) dir;
B. We can use the forced conversion syntax to convert one int type data to its corresponding enumerated value.
Example: int I = 3;
Direction dir = (Direction) I;
C. You can manually specify the corresponding int type data for each enumerated value. When declaring the enumerated value, assign a value to it to specify the number of int types.
5> convert string to Enumeration type
1> case sensitive
(Converted Enumeration type) eum. parse (typeof (converted Enumeration type), string to be converted)
2> case insensitive
(Converted Enumeration type) eum. parse (typeof (converted Enumeration type), string to be converted, true)
6> enumeration is a value type. We generally define enumeration at the same level as the class, because the entire namespace can access it. Of course, it can also be defined in the class.
5. Structure
1> the structure and class are very similar. Fields, methods, attributes, and constructors can be defined in the structure. (Because the structure is inherited in C, and C is process-oriented)
2> the struct also needs to create an object to call the members in the structure. the difference with the class is that the struct can be created by using the new keyword or not by using the new keyword, declare a variable of the structure type,
This variable is an object (the structure is very similar to the class, not the same, or there is a syntax difference ). It is called by an object name point member.
3> statement Syntax:
[Access modifier] struct structure name
{
Structure member;
}
4> Differences Between Structures and classes:
A. The class is the class declaration. The struct is the struct declaration.
B. Although fields can be declared in the struct, fields cannot be assigned at the same time. Otherwise, an error is returned.
C. Although the constructor can be defined in the struct, you cannot write a constructor without parameters, because in any case, the compiler will generate one non-parametric constructor for the struct.
D. In the struct constructor, each field of the struct object must be assigned a value.
E. when creating a struct object. you can use the new keyword to create. however, I may not use the new keyword. use the New Keyword to create an object: all fields in the struct already have values. you can use it directly. do not use the new key to create objects:
The field in the struct has no value. Therefore, you must assign a value to the field before using the value of the field. the reason is: The New Keyword is used to create an object and the constructor calls the constructor. The constructor of the struct must assign values to all fields, even if it is a self-generated none.
Parameter Constructor (assign default values to all fields ). therefore, the new keyword is used to create a struct object. All fields of this struct object have values. if one struct variable is declared without the New Keyword, the constructor is not called,
Therefore, the field has no value.
F. assign values to the attribute in the constructor. The Compiler does not consider it as assigning values to the field because the attribute is not necessarily in the operation field.
G. struct is a value type. Class is a reference type.
5> struct is a value type. When creating struct objects, struct objects are directly stored in variables. When struct variables assign values to each other, the values of fields are copied one by one.
6. Garbage Collection (GC)
1> WE declare variables and create objects to open up space in the memory. Because the memory is limited, these spaces must be recycled. If they are not recycled, they will crash and cause memory overflow. This is the meaning of garbage collection.
2> C # hosting Code does not need to worry about memory reclaim, because the system and. NET Framework will help us complete these tasks.
3> the local variables in the stack space will be immediately recycled by the system when the main character that the program executes ends.
4> when no variable points to a heap object, it is marked as a spam object. Wait for the garbage collector (GC) to be recycled. GC belongs to CLR. GC garbage collection frequency is controlled by Clr and cannot be intervened by programmers.
5> GC. Collect (); Immediately ask GC to recycle the garbage objects in the heap space.
6> if you want to recycle the local variables of the stack space in advance, you must add "{}" to the collected local variables, for example:
{
Int A = 100;
Console. writeline ();
In this case, if A is used only once, then a is immediately recycled.
}
VII. destructor (Terminator function)
1.> Syntax:
~ Class Name ()
{
Method body;
}
2.> The Destructor cannot contain parameters or access modifiers. The Destructor is automatically executed when the object is recycled by GC;
3.> when an object is created, the constructor is executed and the Destructor is executed when the object is recycled. The role of the constructor is: we usually do some aftercare.