C # key Knowledge (iv)

Source: Internet
Author: User
In the fourth chapter: C # box and go box

There are two types of C # runtimes: Reference types (reference in C #) and value types (value) (struct declarations in C #). References and value types differ in several important ways. The value type "feels" like a data. It includes predefined numeric types such as int, bool, and user-defined types (circle, point, and so on). As described above, a variable of a value type is the actual value, so when you use a variable, you typically work with the actual value.


1&gt: First, let's look at the value type (value), which is declared in C #.

For any type of non frame mechanism is also the shape of the bottom.
//-------------------------------------
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 take a look at what the final result is? The result is 10,20. After you specify a variable for the second time, two separate variables contain the same value.
Modifying the value of P does not change the value of F.

2&GT: Reference type is used for all objects that cannot be used as value types. A variable of a reference type points to an instance of an 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, the form is the same as the following.
//------------------------------------------------------
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 take a look at what the final result is? Is it strange that the result is 20,20. After you specify the variable for the second time, p and F point to the same object. This means that modifying the name of P also changes the name of F because they refer to the same instance. A member that modifies a class value is called a "change person", and a class that does not have any of the changes is called an immutable class. The existence of immutable classes can make the behavior of a class similar to a value class, but cannot be written as a value class.

It is important to use both the reference and the value two types in the C # language. Value types are lightweight and efficient, and reference types are appropriate for object-oriented development. However, although we have two types, sometimes we need a simpler model that uses a single type that can encompass all possible values. Such a universal base class can invoke virtual functions of any value. Writes a collection class that can store any value. To achieve this, the C # language runtime takes a method of converting a value type to a reference type when it is needed, that is, through a process called a box. The box type is a universal base class that can be referenced by various types of objects.


Solution Box

int i = 123;
Object k = i;//int i box to Objects K
int j= (int) k; Solution Box K to value2



When assigned to K, as part of the assignment, the C # compiler creates enough reference type wrappers to hold int in the heap, copies the value to the box, and then marks the box as the actual type so that the runtime knows the type of the box. To take a value from a box, you must specify the type of the box (the object can retain any type) by using a mandatory type replacement. During execution, the runtime checks whether the type referenced by the object variable is a mandatory type conversion
The type specified in the If the type is correct, the value is copied back to the value type variable from the box. If the type is incorrect, an exception is caused. Note that no other conversions will be made during the box removal, and the type must match exactly.

Please note the following code:

Long i = 123;
Object k = i;//Adds a long I box to Objects K
ULONG j= (ULONG) K;

#error

Error occurs because the box type is different from the type of the solution. It is also wrong to think that the following actions will be correct, as in the C + + language.

Long i = 123;
Object k = i;
int j= (int) k;

#error


Finally summed up the box and the box. The box and the box make it simple and straightforward to write and use functions that have common object parameters.






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.