C # detailed explanation of key language knowledge (4: box and box)

Source: Internet
Author: User

Chapter 4: adding and removing boxes in C #

C # There are two types in the runtime: reference (class declaration in C #) and value (value) (Structure declaration in C ). The reference and value types vary in several important aspects. The Value Type "feels" like a data. It includes predefined numeric types (such as int and bool) and user-defined types (such as circle and Point ). As described above, variables of the value type are actual values, so when you use variables, the actual values are usually processed.


1>: first, let's take a look at the value type (value) (using structure declaration in C ).

For any type of non-box mechanism, the following is the form.
//-------------------------------------
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 final result is? The result is 10, 20. After the second variable is specified, two independent variables contain the same value.
Changing 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. Variables of the reference type point to the instance of the object in the heap. This means that when you specify a variable
For another variable, the reference is specified, not the value.

For any type of Box classes, the following is the form.
//------------------------------------------------------
Class T_Point
{
T x, y;
T_Point (T x, 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 final result is? Is it strange that the result is 20, 20. After the second variable is specified, p and f point to the same object. This means that changing the name of p will also change the name of f because they reference the same instance. The member who modifies the class value is called the "modifier", and the class that does not have any modifier is called the immutable class. The existence of an immutable class can make the class behavior similar to a value class, but cannot be written as a value class.

It is important to use both the reference and value types in the c # language. The value type is lightweight and efficient, while the reference type is suitable for object-oriented development. However, although we have two types, sometimes we need a simpler model that uses a single type that can include all possible values. Such a general base class can call virtual functions of any value. Write a collection class that can store any value. To achieve this goal, c # uses a method to convert a value type to a reference type when needed, that is, a process called a box. The type of the box to be added is a common base class, which can be referenced by various types of objects.


Box

Int I = 123;
Object k = I; // Add int I to object k
Int j = (int) k; // describe box k to value2

 

When a value is assigned to k, as part of the value assignment, the C # compiler will create a reference type package that is sufficient to accommodate the int In the heap, copy the value to this box, and then mark the box as the actual type, to understand the type of the box during running. To take a value from the Ampersand, you must use the forced type package to specify the Ampersand type (the object can retain any type ). During execution, the runtime checks whether the type referenced by the object variable is forced type conversion.
. If the type is correct, the value will be copied back to the value type variable from the Plus box. If the type is incorrect, an exception occurs. Note that no other conversions will be performed during the unboxing process. The type must be exactly the same.

Note the following code:

Long I = 123;
Object k = I; // Add long I to object k
Ulong j = (ulong) k;

# Error

An error occurs because the type of the box is different from that of the box. If we think that the operations in the c ++ language will be correct, it is also wrong.

Long I = 123;
Object k = I;
Int j = (int) k;

# Error


Finally, I will summarize the box and the box. The box and box show make it simple and straightforward to write and use functions with common object parameters.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.