[Brief Description :]
This chapter focuses on the Value Type and reference type. The Value Type describes BigInteger and Nullable <T>, and the reference type describes String. Associate the Object type value type with the reference type. Finally, I talked about the extensive generic type.
Section 1 Use of the numeric type
1. Automatic and forced conversion of value types
When the value type is changed from "large to small"-for example, if the double type is converted to the float type, forced conversion is required, which may cause data loss. Therefore, the code is considered as a defect code, you only need to select the appropriate data type based on the value size.
[Case]
Int32 I = 0;
Float f = float. MaxValue; // maximum value of the float type
I = (Int32) f;
Console. WriteLine (I );
Program output result:-2147483648
2. Calculation Based on astronomical numbers
To facilitate the calculation of "astronomical numbers", the. net4.0 class library introduces the big Integer type --- BitInteger
[Case notes in the book]
A. The BigInteger type is defined in System. Numerics.
B. Fibonacci series: refers to the Fibonacci series, 1, 1, 2, 3, 5, 8, 13, 21 ,......
C. When it is defined as long and the number of 93rd Fibonacci values is calculated, OverflowException is triggered (An Arithmetic overflow exception ).
When it is defined as BigInteger, it does not appear.
D. A Class of astronomical numbers with decimal points can be designed based on BigInteger.
Section 2 use variables of the reference type
1. Thread stack and managed Stack
A. process: it is an independent unit for the system to allocate and schedule resources.
B. Thread: it is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a basic unit that can run independently less than a process. Each Independent thread has a program running entry, sequence execution sequence, and program exit. Multithreading means that multiple execution parts in an application can be executed simultaneously.
C. Thread Stack: Each thread has a reserved location, which is called a thread stack. Value Type variables are allocated in the memory thread stack.
D. managed heap: The other memory area is called a heap. In the. net managed environment, the heap is managed by the CLR. Therefore, it is called a managed heap.
The memory occupied by the object referenced by the reference type variable is allocated in the managed heap.
2. Reference The Memory Model of the Type Variable
A. the reference type variable exists in the thread stack, and the referenced object exists in the managed heap. There are four types of reference: class type, interface type, array type, and delegate type.
B. [case notes in the book]
Objs = new MyClass [10] indicates that 10 reference type variables are created, and the value of each variable is null. It does not create 10 MyClass objects. To create an object, you need obj [I] = new MyClass () {Value = I };
Note: This object array reference (10 referenced variables are created in the thread stack, rather than in the managed heap.
3. Analysis of "=" and "="
A. "=" is the value assignment operator, and "=" is the relational operator.
B. Mutual assignment of two referenced type variables means that the content of the memory units occupied by the two introduced values is the same.
C. when defining a reference type variable without referencing any object, you must assign it a null value. Otherwise, compilation cannot be generic.
D. When the value type is applied to "=", it is to compare whether the values of the two variables are equal. When the variables are referenced, it is to compare whether the two referenced variables reference the same object.
E. The string class "=" compares the value of the object. Does it violate the rules? No, because it is overloaded with "=.
Section 3 value type variables
1. The structure is similar to the class. You can also have methods and constructor. the CLR does not need to automatically call the constructor of the structure. It must be explicitly called using NEW.
MyPointF my = new MyPointF (100,200 );
Console. WriteLine (my. ToString ());
2. When should I use struct?
Struct is used when the amount of information to be encapsulated is small and can be expressed by value type fields.
3. Because the value type inherits from the object, you can use the packing and unpacking operations to regard the value type variable as a reference type variable, but it will affect program performance, therefore, try to avoid using it in the program.
4. Why does the packing operation significantly affect the performance?
A: It is preferred to allocate memory in the managed heap, create anonymous objects, and then copy data.
Endless strings in Section 4
1. String is a unique reference type instead of a value type. This can be confirmed by the Object Browser.
2. assign a value to a String variable of the Image value type. String s1 = "abcd"
3. The content of the String variable is readable. Each time a value is assigned, a new String is created instead of being expanded in the original memory area. String s1 = "a"; string s1 = "abcd"
4. The "+" operator of string actually calls the Concat method.
5. Introduce the resident pool, which contains the string constants declared by the program programmatically or displays the object references added to the pool. It must be introduced to improve the performance of string operations.
6. string usage suggestion: Avoid using addition operators to connect different types of data. Use StringBuilder in a loop instead of String to implement string connection. I lamented that StringBuilder has a very good performance, especially when the number of loops is large.
Section 5 "Value Type" for a null value"
1. System. Nullable <T> is called an empty type, T? It is a value type.
2. If null types are involved in calculation, the result is also null.
3. When the null type is assigned to the value type, you can do this:
Int? Op1 = null;
Int result = (op1 ?? 0) * 2; // equivalent to op1.GetValueOrDefault (0) * 2;
4. Main purpose: it is convenient to use DataTable or DataReader to assign values to value-type variables.
Section 6 templated data types-generic
1. The where clause in a generic definition is qualified as a "generic type parameter constraint ".
2. The dynamic program listed in the expanded reading by the author has an error, because the program is abnormal if ">" operator overload is not implemented if a custom type is not implemented!
3. Manually designed plural classes include generic classes, generic methods, generic structures, and generic interfaces.
4. Advantages of generics:
A. Reduce the number of classes
B. Structure and algorithm of split data
C. To improve the program running efficiency, the generic List <int> is several times more efficient than ArrayList.
D. Reduce coding errors