C # Series 3 C # data types and variables 2

Source: Internet
Author: User

The previous article introduced the basic data type. Today we will introduce the value type and reference type!


1. What is the value type and reference type?

C # Like most data types in object-oriented languages, are there two types of data: Value Type and reference type? How can we differentiate these two types?

Based on the type structure diagram, we can conclude that:

1. value types include built-in data types such as (INT double float) User-Defined value types (struct) enumeration (emun)

2. The reference type includes the interface type and class type.

After knowing that those types belong to the value type and those types belong to the reference type, let's analyze what is the difference between the value type and the reference type?

First, continue to see the figure:

We can see that our computer memory can be divided into two areas: heap (. NET is a managed heap) and stack! After understanding this, we can see the difference between the value type and the reference type:

1 Value Type: basic data types such as int double float. The actual values are allocated to the stack!

2. Reference Type: the actual values of the reference type, such as object, are all allocated to the stack and the stack stores a value address.

For exampleCode:

1 // Allocate a space on the stack to store 10
2 Int A =   10 ;
4 // Allocate a space on the stack to store the value 10 of variable A, and allocate a space on the stack to store the value in the heap address.
5 Object O = A;

After understanding the concepts of basic value types and reference types, we will analyze their nature in depth.


Analysis of binary and reference types

1 Value Type

The value type is allocated to the stack. According to the stack principle, once the value type leaves the currentProgramWill be destroyed immediately! So what if I define two identical value types? See the following code:

Static   Void Main ()
// At this time, allocate a space in the stack to store 10
Int A =   10 ;

// Allocate another space in the stack to store the value of a 10.
Int B = A;

// change 10 of the space in B to 20
// This does not affect a because both of them have independent space
B = 20 ;

//Print the values of A and B
Console. writeline ("A = {0}", A. tostring ());
Console. writeline ("B = {0}", B. tostring ());
}//Destroy the address space allocated by A and B at the end of the Scope

Running result:


2 reference type

The actual value of the reference type is stored in the heap, and the address for allocating space in the heap is allocated in the stack. So what happens when the reference type leaves the scope of the object? Refer to the following code:

1 Class Myclass
2 {
3 Public Int A;
4 }
6 Class Program
7 {
8 Static   Void Main ( String [] ARGs)
9 {
10 // At this time, allocate a space for 10 in the heap, and allocate a space for 10 in the stack.
11 Myclass mc1 =   New Myclass ();
12 Mc1.a =   10 ;
14 // In this case, another 20 space is allocated in the heap, and a 20 space address is allocated in the stack.
15 Myclass mc2 =   New Myclass ();
16 Mc2.a =   20 ;
18 // Assign the stack address in A to B.
19 // In this case, B points to the address allocated by a in the heap.
20 // At this time, 20 of the heap space address that B Originally pointed to was discarded.
21 Mc1 = MC2;
23 // Modify the heap space pointed to by both A and B to 30
24 Mc2.a =   30 ;
26 // Print results
27 Console. writeline ( " Mc1.a = {0} " , Mc1.a );
28 Console. writeline ( " Mc2.a = {0} " , Mc2.a );
30 }
31 }

The running result is as follows:


I will introduce packing and unpacking in the next article.

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.