I. C # is a strongly typed language, that is, each object has only one type. This type has been defined when the object is created and remains unchanged during the execution of the object. All variables of C # Must be initialized before use.
Ii. Managed thread Stack
1) Each windows thread has a private memory area called a stack. The role of the thread stack is
I. Save the passed real parameter value of the method being executed;
Ii. Save the address of the local code to be redirected when the method is returned;
Iii. Save the object
2) The stack size is variable, usually limited to 1 MB.
Iii. managed heap
A process has only one heap, which is a memory area in the process address space. All threads in the Unified Process can access this region. That is to say, the heap is different from the stack and does not belong to a specific thread. The main function of heap is to store objects. In addition, the size of the heap is the same as that of the stack, which can be changed during execution. However, the size of the heap is much larger than that of the stack.
Iv. Object Storage |: Comparison between managed thread stack and managed Stack
1) the advantage of heap is that its capacity is much larger than that of stack.
2) the advantage of the stack is that the access speed is faster than that of the stack. This is mainly due to the dedicated access to the stack's IL commands. Another reason is that the elements on the access page do not need to be synchronized.
3) Therefore,. NET is designed to store large objects with stacks to store small objects.
V. Comparison between static allocation and Dynamic Allocation
1) the consistency between C ++ and C # is that the object is either allocated to the thread stack (we call it static allocation ), either be allocated to the process stack (we call it Dynamic Allocation)
2) The difference between C ++ and C # Lies in the method of selecting the allocation mode.
In I. C ++, the object allocation mode is automatically selected by the programmer. Objects directly declared in the Code use static allocation, for example (int I = 0 ). Dynamic Allocation is used when the object uses the new operator (bit int * pi = new int (0)
The object allocation mode in ii. C # depends on the object implementation mode. Value-type instances use static allocation, while reference-type instances use dynamic allocation.
Iii. C # programmers are less responsible than C ++ programmers.
- You do not need to select an object allocation method.
- You do not need to worry about unallocation of objects.
Vi. Reference Type and Value Type
1). NET each type is either a value type or a reference type. Value-type instances are usually allocated on the thread stack, but in some cases they can be stored in the heap. Objects of the reference type are always allocated in the process heap (Dynamic Allocation)
VII. CTS for public systems
1) Basic Type: integer, floating point number, character, Boolean, AND OTHER TYPES
2) Enumeration
3) Structure
4) Class: all are reference types. Pay special attention to the system. string type of the string and the system. array type of the array.
5) delegate type. Its instance can apply a method, acting as a function pointer of C ++
6) pointer.
8. Object comparison
1) objectA. equals (objectB)
2) object. ReferenceEquals (objectA, objectB)
IX. Object Replication
1) if it is a value type instance, then the "=" replication operator and iukeyi copy the source object status to the target object step by step. For an instance of the reference type, the "=" value assignment operator only copies the reference, not the object itself. Therefore, the reference type requires a method to copy the object state of the reference type. The System. IConeable interface is specially prepared for this work.
2) memberwiseclone () method
Class Article {
Public string Description;
Public int Price;
}
Class Order: System. ICloneable {
Public int Quantity;
Public Article;
Public override string ToString (){
Return "Order:" + Quantity + "x" + Article. Description +
"Total cost:" + Article. Price * Quantity;
}
Public object Clone (){
// Light copy
Return this. MemberwiseClone ();
}
}
Class Program {
Static void Main (){
Order order = new Order ();
Order. Quantity = 2;
Order. Article = new Article ();
Order. Article. Description = "Shoes ";
Order. Article. Price = 80;
System. Console. WriteLine (order );
Order orderClone =Order.Clone() As Order;
OrderClone. Article. Description = "Shirt ";
System. Console. WriteLine (order );
}
}
The result of the above example is:
Order: 2 * shoes total cost: 160;
Order: 2 * shirt total cost: 160;
We can see that the changes to the items in the cloned order are reflected in the items in the original order. This is because there is only one Article class instance in this program, and both order Classes reference this unique instance.
3) Deep Replication
Class Article: System. ICloneable {
Public string Description;
Public int Price;
Public object Clone (){
//In this example, the light copy is equal to the deep copy.
Return this. MemberwiseClone ();
}
}
Class Order: System. ICloneable {
Public int Quantity;
Public Article;
Public override string ToString (){
Return "Order:" + Quantity + "x" + Article. Description +
"Total cost:" + Article. Price * Quantity;
}
Public object Clone (){
// Deep copy
Order clone = new Order ();
Clone. Quantity = this. Quantity;
Clone. Article = this. Article. Clone () as Article;
Return clone;
}
}
Class Program {
Static void Main (){
Order order = new Order ();
Order. Quantity = 2;
Order. Article = new Article ();
Order. Article. Description = "Shoes ";
Order. Article. Price = 80;
System. Console. WriteLine (order );
Order orderClone = order. Clone () as Order;
OrderClone. Article. Description = "Shirt ";
System. Console. WriteLine (order );
}
}
The result of the above example is:
Order: 2 * shoes total cost: 160;
Order: 2 * shoes total cost: 160;
In this example, the shallow replication of the article class is equivalent to the deep replication. We can conclude that for a given class, its shortest replication is equivalent to the deep copy operation only when all Members are of the value type. However, a field in the article class is of the string type, but it is of the reference type, but the string class has special properties. One of them is that its instances cannot be changed. This feature makes strings and value types very similar in many scenarios.
4) copy constructor: This constructor accepts the type of the copy we want to use as a parameter.
Class Article: System. ICloneable {
Public string Description;
Public int Price;
Public object Clone (){
Return this. MemberwiseClone ();
}
}
Class Order {
Public int Quantity;
Public Article;
Public override string ToString (){
Return "Order:" + Quantity + "x" + Article. Description +
"Total cost:" + Article. Price * Quantity;
}
// Default constructor.
Public Order (){}
// Copy the constructor
Public Order (Order original, bool bDeepCopy ){
This. Quantity = original. Quantity;
If (bDeepCopy)
This. Article = original. Article. Clone () as Article;
Else
This. Article = original. Article;
}
}
Class Program {
Static void Main (){
Order order = new Order ();
Order. Quantity = 2;
Order. Article = new Article ();
Order. Article. Description = "Shoes ";
Order. Article. Price = 80;
System. Console. WriteLine (order );
Order orderClone = new Order (order, true );
OrderClone. Article. Description = "Shirt ";
System. Console. WriteLine (order );
}
}
10. packing and unpacking
1) instances of the value type that are used as local variables of the method are directly stored on the online process stack. Instances using these value types in this thread do not need to be referenced by pointers. Some methods require parameters of the reference type object class. All value types are derived from the object, but the value type instance is not referenced. This process is packed.
Class Program {
Static void f (object o ){}
Public static void Main (){
Int I = 9;
F (I );
}
}
The above instance runs without errors. It is the packing that allows us to obtain a reference to a value type instance that is not referenced. The installation operation is completed in three steps:
- This value type creates a new instance and is allocated to the heap.
- Instances in this heap are initialized based on the status of instances in the stack. In the above example, our certificate is replicated in four bytes. We can say that the initial object instance has been cloned.
- Use this to point to the newly created instance reference to replace the instance originally allocated in the stack.
2) The opposite process is unpacking.
Class Program {
Public static void Main (){
Int I = 9;
Object o = I; // I is boxed
Int j =(Int)O; // o is unboxed
}
}
3) binning and unpacking in C # are implemented implicitly.
11. Basic Types
1) C # No unsigned keyword. byte, ushort, uint, and ulong are used to represent unsigned integers.
In c #, the size of long and ulong is 8 bytes.
In C #, decimal 16 bytes can represent precise real numbers of valid numbers up to 28 characters
In C #, short is 2 bytes.
In C #, int Is 32 bits.
2) minor issues with code
Int I = 1000000000 // I = 1 billion
Long j = 10 * I;
The result j is not 10 billion. In fact, this calculation is based on the int type (10 is int type), so the result is copied to a long variable. The biggest problem here is that developers will not receive any warning unless the checked keyword is used. To solve this problem, notify the compiler to use an 8-byte integer by adding L to the end of the literal constant.
Using System;
Class Program {
Public static void Main (){
Int I = 1000000000;
Long j = 10L * I;
Console. WriteLine (j );
}
}
12. Calculation of Basic Types
1) five basic arithmetic operators +-*/%
2) 5 value assignment operators corresponding to basic Operators
I + = j; I = I + j;
I-= j; I = I-j;
I * = j; I = I * j
I/= j; I = I/j;
I % = j; I = I % j;
3) operator priority
I. +-×/has a priority higher than %
Ii. You can use parentheses to increase the operator priority.
Iii. When arithmetic operations are performed between two different basic types of variables, the result type is one of the two types with a large value range.
Iv. Force type conversion is allowed between any integer and floating point type. You can use the checked keyword.
4) bitwise operation
I. <left shift is equivalent to multiplication 2
Ii.> right shift equals to Division 2
Iii.
13. Structure
1) The structure is a value type, and the instance is stored in the stack, so the structure is not suitable for too large. It is best to replace a large structure with a class;
2) The structure cannot inherit from other classes or structures, nor be used as the base class of other derived classes or structures;
3) Unlike the class field, the structure field cannot display initialization in the Declaration;
14. Enumeration and shaping
1) by default, the compiler sets the enumerated value to an integer of the int type;
2) The Object. Tostring () has automatically overwritten each enumeration. The function is to return the string of the name when the enumeration constant is defined;
3) System. Enum class
String [] GetNames (Type type), returns a String array of names of all values in the enumerated Type
Class Program {
Enum Maker {Renault, Ford, Toyota }
Static void Main (){
Foreach (string s in System. Enum. GetNames (typeof (Maker )))
System. Console. WriteLine (s );
}
}
15. String
1) Escape Character :\
2)Double quotation marks \",Backslash \, empty character \ 0, carriage return character \ r, horizontal tab \ t, single quotes \'
3) No escape String constant: @ will accept all the line breaks in the String constant. This feature is useful when generating code.
4)
16th,System. Text. StringBuilderClass
1) Stringbuilder append () adds characters to the end of the string
2) Stringbuilder insert (int index,) inserts characters into the position specified by index. If the position is 0, the data is inserted to the string header.
3) Stringbuilder remove (int startindex, int length) deletes the string from startindex to startindex + length. If one of the two locations is smaller than 0 or the length of the string is smaller than 0, argumentoutofrangeexception is thrown.
Class Program {
Static void Display (System. Text. StringBuilder s ){
System. Console. WriteLine ("The string: \" {0} \ "", s );
System. Console. WriteLine ("Length: {0}", s. Length );
System. Console. WriteLine ("Capacity: {0}", s. Capacity );
}
Public static void Main (){
System. Text. StringBuilder s = new System. Text. StringBuilder ("hello ");
Display (s );
// The string: "hello"
// Length: 5
// Capacity: 16
S. Insert (4, "-- salut --");
Display (s );
// The string: "hell -- salut -- o"
// Length: 14
// Capacity: 16
S. Capacity = 18;
Display (s );
// The string: "hell -- salut -- o"
// Length: 14
// Capacity: 18
S. Replace ("salut", "hello everybody ");
Display (s );
// The string: "hell ~ ello everybody -- o"
// Length: 24
// Capacity: 36
S. EnsureCapacity (42 );
Display (s );
// The string: "hell -- hello everybody -- o"
// Length: 24
// Capacity: 72
}
}
17. Delegate class and delegate object
1) C # allow the use of the delegate keyword to create a special class, which is called the delegate class. The instance of the delegate class is calledDelegate object.
In terms of concept, delegation is a reference pointing to one or more methods (static or non-static. We can call the delegate object using the syntax of the call method, which will call the method referenced by the delegate object.
2) Commission speculation
Public class Program {
Delegate void Deleg1 ();
Delegate string Deleg2 (string s );
Static void f1 (){
System. Console. WriteLine ("f1 () called .");
}
Static string f2 (string s ){
String _ s = string. Format ("f2 () called with the param \" {0} \ ".", s );
System. Console. WriteLine (_ s );
Return _ s;
}
Public static void Main (){
// Check the IL syntax. In fact, the constructor of the deleg1 and deleg2 delegate classes is called.
Deleg1 d1 = f1; // replace Deleg1 d1 = new Deleg1 (f1 );
D1 ();
Deleg2 d2 = f2; // replace Deleg2 d2 = new Deleg2 (f2 );
String s = d2 ("hello ");
}
}
3) delegate object and instance method
The delegate can reference the instance method in the same way.
Using System;
Public class Article {
Private int m_Price = 0;
Public Article (int price) {m_Price = price ;}
Public int IncPrice (int I ){
M_Price + = I;
Return m_Price;
}
}
Public class Program {
Public delegate int Deleg (int I );
Public static void Main (){
// Create an article with a price of 100.
Article article = new Article (100 );
// Create a delegate object that references the operator ncPrice () operator?
// Method on the object before using rticle?
Deleg deleg = article. IncPrice;
Int p1 = deleg (20 );
Console. WriteLine ("Price of article: {0}", p1 );
Int p2 = deleg (-10 );
Console. WriteLine ("Price of article: {0}", p2 );
}
}
4) System. Delegate class
In fact, when a delegate object references multiple methods, each method needs to create a System. an instance of the Delegate class. In fact, an instance of the MulticastDelegate class can be considered as a System. list of Delegate instances.
Learn the following code
Using System;
Public class Article {
Public int m_Price = 0;
Public Article (int price) {m_Price = price ;}
Public int IncPrice (int I ){
M_Price + = I;
Return m_Price;
}
}
Public class Program {
Public delegate int Deleg (int I );
Public static void Main (){
Article a = new Article (100 );
Article B = new Article (103 );
Article c = new Article (107 );
// Deleg points to (a. IncPrice, B. IncPrice, c. IncPrice ).
Deleg deleg = a. IncPrice;
Deleg deleg1 = B. IncPrice;
Deleg1 + = c. IncPrice;
Deleg + = deleg1;
Deleg (10 );
Console. WriteLine ("a: {0} B: {1} c: {2 }",
A. m_Price, B. m_Price, c. m_Price );
// Try to remove the sub-table (a. IncPrice, c. IncPrice)
// Not in the delegate object deleg
Deleg deleg2 = a. IncPrice;
Deleg2 + = c. IncPrice;
Deleg-= deleg2;
Deleg (10 );
Console. WriteLine ("a: {0} B: {1} c: {2 }",
A. m_Price, B. m_Price, c. m_Price );
// Try to remove the sub-table (a. IncPrice, B. IncPrice) that is included in the delegate object deleg
Deleg deleg3 = a. IncPrice;
Deleg3 + = B. IncPrice;
Deleg-= deleg3;
Deleg (10 );
Console. WriteLine ("a: {0} B: {1} c: {2 }",
A. m_Price, B. m_Price, c. m_Price );
}
}