I recently learned the. NET Framework 2.0 advanced programming book, which is quite interesting. So I took notes and summarized the content based on my own understanding. This document mainly describes the. NET type, generic type, and set.
Content:
1. Type
2. Generic
3. Set
1. Type
Type is the type of data objects to be processed by the program. Different data objects occupy different storage space and have different processing methods. Therefore, they must be categorized;
(1) The main types of C # are:
Basic types: Numeric value, character, logical type, String, object)
System or custom type: structure, enumeration, class, and so on.
These types occupy different memory spaces. Some types of memory usage are definite, while others are uncertain.
Depending on the storage space they are allocated, they can be divided into two categories: Value Type/reference type.
In the above types, can you tell which category they belong?
Most of the basic data types are value types. Except for object, string belongs to the reference type, class is the reference type, and structure and enumeration are the value type.
(2) memory usage: stack and heap static zones ).
Value types are directly allocated to stacks;
The reference type includes two parts: the reference (address) of the object is on the stack, and the object itself is on the heap.
The static area stores the parts that are not related to the object's instance. That is, when the program loads the memory, it needs to be allocated.
For example, the entry method, constructor method, constant, and other static modified members.
(3) Example:
Example 1:
Int x, y = 0;
X = y;
Y = 9;
Example 2: The class Myobj is defined:
Class Myobj
{
Public int age;
Public string name;
}
Myobj a, B;
A = new Myobj ();
A. age = 28; a. name = "puppy ";
B =;
A. age = 18; a. name = "doggy ";
Console. WriteLine (B. age );
Console. WriteLine (B. name );
Example 3:
String x, y = "string1 ";
X = y;
Y = "string2 ";
Console. WriteLine (x );
In the first example, the output is 0. When y = 9, a memory address is actually re-allocated in the stack.
The second example outputs 18 and doggy. When B = a, both a and B point to an address space.
The output in the third example is string1. String is a special reference type. The string instance cannot be modified in the memory. When you need to modify the instance, you always create a new instance and point the variable to the new instance.
(Frequent modification of string variables in the program will produce a large amount of memory garbage. This is why we want to use the StringBuilder class)
(4) packing and unpacking
Packing: Convert the value type to the reference type, and generate a new object in the heap according to your own understanding. The original address in the stack points to this object.
Binning: converts a reference type to a value type, which is roughly the same as the preceding one. The process is the opposite.
Int x = 1;
Object y = x; // boxed
Int z = (int) y;/unpack
2. Generic
Cause:
See a Stack program. Stack is a data structure. It can store many elements and follows the principle of "first-in-first-out.
Example: (Stack)
Class StackOfInt {
Private int [] m_ItemArray;
Private int m_Index = 0;
Public const int Max_Size = 100;
Public StackOfInt () {m_ItemArray = new int [Max_Size];}
Public int Pop (){
If (m_Index = 0)
Throw new System. InvalidOperationException ("Can't
Pop an empty stack .");
Return m_ItemArray [-- m_Index];
}
Public void Push (int item ){
If (m_Index = Max_Size)
Throw new System. InvalidOperationException ("Can't
Push an item on a full stack .");
M_ItemArray [m_Index ++] = item;
}
}
Disadvantage: this stack uses the object type as the element type and can be used by all types.
However, when an element is pressed into the bin, it needs to be packed; when elements are used, the bin should be removed, which is costly and inefficient in execution;
Type is not safe. Errors may occur when converting data types. Www.2cto.com
Solution: If we can provide a class as a parameter in the Stack class definition, the solution to this problem can be simplified. Generic definition: when defining a type, use another or more types as parameters. type parameters are enclosed by <> and placed behind the defined type name.
When using a generic type with a type parameter, you must also specify the specific type of the parameter type.
Example: define a generic Stack class
Class Stack <T> {
Private T [] m_ItemArray;
Private int m_Index = 0;
Public const int Max_Size = 100;
Public Stack () {m_ItemArray = new T [Max_Size];}
Public T Pop (){
If (m_Index = 0)
Throw new System. InvalidOperationException ("Can't
Pop an empty stack .");
Return m_ItemArray [-- m_Index];
}
Public void Push (T item ){
If (m_Index = Max_Size)
Throw new System. InvalidOperationException ("Can't
Push an item on a full stack .");
M_ItemArray [m_Index ++] = item;
}
}
Advantages of generics:
Make the code more universal and concise;
Strong type, type security, do not worry about type conversion error;
You don't have to waste a lot of time packing and unpacking.
3. Set
A collection type is a special type of an object that contains multiple other objects.
Arrays are the most common collection types.
It should be further understood that when we declare an array in the program, it actually instructs CLR to help us create and manage a collection type during execution;
The base class of the set type is System. Array.
Therefore, we can use some inherent attributes and methods for arrays, such as Length, Rank, Copy, and Clone. They are from the Array class. They can also use foreach to traverse arrays. This is because Array has implemented the IEnumerable interface.
Therefore, we can remember that arrays are of the reference type. You can use an array as a method parameter. If you do not use ref, you can also obtain the Modification result from the method.
Example:
Using System. Collection. Generic;
Public class Program {
Public static void Main (){
List <int> list = new List <int> (3 );
For (int I = 0; I <8; I ++ ){
List. Add (I );
System. Console. WriteLine ("Count: {0} Capacity: {1 }",
List. Count, list. Capacity );
}
List. TrimExcess ();
System. Console. WriteLine ("Count: {0} Capacity: {1 }",
List. Count, list. Capacity );
}
}
From ForEvErNoMe