C # Key knowledge detailed (2)

zhaozj2021-02-11  192

Chapter 2 Memory Management

C # memory management provides the same automatic memory management function as Java, allowing programmers to get rid of heavy memory management, and memory management improves the quality of code and improves development efficiency.

C # limits the use of the pointer, eliminating the troubles of programmers, but does not mean that C # programmers do not use the benefits of pointers like the Java programmers. Microsoft takes this question when designing C # language, and on the one hand, while discarding the pointer, on the other hand, it uses a compromised approach to introduce a pointer through a logo.

First let's understand the automatic memory management

Public class stack {private node first = null;

Public bool empty {get {return (first == null);}}

Public Object Pop () {if (first == NULL) throw new Exception ("Can't Pop from an Empty Stack."); Else {Object Temp = first.Value; first = first.next; return temp;}}

Public void Push (Object O) {first = new node (o, first);

Class node {public node next;

Public Object Value;

Public Node (Object Value): this (value, null) {}

Public Node (Object Value, Node next) {next = next; value = value;}}}

The program creates a Stack class to implement a chain, use a PUSH method to create a Node node instance and a collector when the Node node is no longer required. A node instance cannot be accessed by any code, it is collected. For example, when a point element is removed, the associated Node is collected.

The Example

Class test {static void main () {stack s = new stack ();

For (int i = 0; I <10; i ) s.push (i);

s = null;}}}}

With regard to the reference to the pointer, use the UNSAFE flag in C # to represent the reference to the team pointer. The following program demonstrates the usage of pointers, but due to the use of pointers, memory management has to be completed.

Using system;

Class test {unsafe static void location (byte [] ar) {fixed (byte * p = ar) {byte * p_lem = p; for (int i = 0; i

Static void main () {byte [] arr = new byte [] {1, 2, 3, 4, 5}; Writelocations (ar);}

转载请注明原文地址:https://www.9cbs.com/read-4497.html

New Post(0)