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

zhaozj2021-02-11  246

Chapter 4: Pixabay and Going on C #

There are two types in C # runtime: Reference Type (Reframe Declaration in C # Class Class) 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.). 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 x, y; t_point (t X, Y) {this.x = x; this.y = y}} // ------------------------------ -------

SAMPLE:

Class test {struct point {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; p.x = 20; console.write ((POINT) f) .x); console.write (p.x);}}

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>: The reference type is used 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, y) {this.x = x; this.y = y}} // ------------- ------------------------------------------ Class test {class point {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; p.x = 20; console.write ((POINT) f) .x); console.write (p.x);}}

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. However, although we have two types, sometimes we need a simpler model, using a single, can cover all types of possible values. Such a generalized base class can call any virtual function. Write a collection class that can store any value. To achieve this, the C # language is running in a way to convert the value type when needed to the reference type, ie, is called the fax process. The type of fax is a generic base class that can be referenced by various types of objects. Unknown

INT i = 123; Object K = i; // Pixabay INT i into object K int j = (int) K; // SQ box k to value2

When assigning K, as part of the assignment, the C # compiler will create a reference type package sufficient to accommodate int, copy the value to the fax, and then marked the plus box as the actual type, so as to run the box Types of. To take a value from the box, you must use the forced type to install the specified type of type (the object can keep any type). During the execution, the type of type reference reference to the object variable will be specified in the mandatory type conversion at runtime. If the type is correct, the value will replicate the value type variable from the box. If the type is incorrect, it will cause an exception. Note that other conversions will not be made during the release of the box; the type must be fully matched.

Please note the following code:

Long i = 123; Object k = i; // Put the long i to the object K ulong j = (ulong) K;

#Error

Because the fax type is different from the difference in the method of unpacking the frame. If you think that the following operations like a C language will be correct.

Long i = 123; Object K = i; int J = (int) k;

#Error

Finally, summarize the boxes and solutions. Putting the box makes it easy to write and use a function with general object parameters.

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

New Post(0)