C # Language Key Knowledge Detailed (4: Pixabay and Follow]

zhaozj2021-02-08  259

Guide: There are two types in C # runtime: Reference Types (Class Class Class Class Declaration) and Value (Structure Declaration in C #). Quote and value types are different in several important aspects. Value type "feel" like a data. It includes predefined numerical types (such as int, bool), and user-defined types (Circle, Point, etc.).

Author: wkrain www.ASPCool.com Chapter 4: runtime in C # C # framed and go box there are two types: reference types (reference) (in C # using the class declaration) and value types (value) (in C # in structure declarations). Quote and value types are different in several important aspects. Value type "feel" like a data. It includes predefined numerical types (such as int, bool), and user-defined types (Circle, Point, etc.). As described above, the variable of the value type is the actual value, so when you use the variable, it usually processes the actual value. 1>: First, let's take a look at the value type (VALUE) (in the C # to declare). For any type of non-frame mechanism, the following shape. // - Struct t_point {t_point (t x, y) {this.x = x; this.y = y}}} // - Sample: Class test {struct point {public int x, y; public {public Int x, y; public Point (int x, int y) {this.x = x; this.y = y;}} public static void main () {Point P = new point (10, 10); Object f = P; px = 20; Console.write ((POINT) f) .x); console.write (px);} Let me see what the last result is? The result is 10, 20. After the second specified variable, the two independent variables contain the same value. Modifying the value of P does not change the value of f. 2>: Reference Type For all objects that cannot be used as value types. The reference type variable points to the instance of the object in the heap. This means that when a variable is specified to another variable, only the reference is specified, not the value. For any type of box class, there is a shape. // - Class T_Point {T x, y; t_point (t_point (THIS.X = x; this.y = y}} // Class test {class point {public Int x, y; public point (PUBLIC POINT) INT x, int y) {this.x = x; this.y = y;}} public static void main () {POINT P = New Point (10, 10); Object f = p; px = 20; console. Write ((POINT) f) .x); console.write (px);} Let me see what the last result is? Very strange, the result is 20, 20. After the second specified variable, P and f point to the same object. This means that the name of modifying P will also change the name of F because they reference the same example. A member of the modified class value is called "Customer" without any modified class. The presence of a non-variable class can make the behavior of the class similar to the value, but cannot be written as a value class. It is important to use both references and values ​​in the C # language. The value type is light and efficient, and the reference type applies to object-oriented development.

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

New Post(0)