To retrieve the differences between basic data types and reference types in 01-c#

Source: Internet
Author: User

1. Basic definition

Basic data types include: integer, float, character, struct, Boolean, datetime, enum type, etc.

Reference types include: strings, classes, arrays, interfaces, and so on

Heap definition: is a special kind of tree data structure, each node has a value, generally by the programmer allocation release, belong to the dynamic allocation, if the programmer does not release, the end of the program may be recycled by the OS

Stack definition: is to follow the linear structure of the last-in-first-out rule in the operation, in the class load is statically allocated by the system, the parameter value of the stored function, the value of the local variable, etc., after the method call is finished, immediately release

2. The difference between a basic data type and a reference type

Primitive Type Values refer to simple data segments that are stored in the stack's memory , meaning that this value is completely stored in a single location in memory.

The reference type value refers to the object that is stored in the heap memory , meaning that the variable is actually just a pointer to another location in memory where the object is saved.

Instances of value types are often stored on the stack. But there are also special cases. If an instance of a class has a field of value type, the field is actually stored in the same place as the class instance, which is the heap. However, objects of reference types are always stored in the heap.

When assigning a value to a variable, the parser must determine whether the value is a base type value or a reference type value. The values of the base data type occupy a fixed amount of space in memory, so they can be stored in the stack memory. For variables that hold basic data types, we can say that they are accessed by value because we are manipulating the values they actually hold.

If you assign a variable to a value of a reference type, you must allocate space for the value in heap memory. Because the size of this value is not fixed, they cannot be saved to the stack memory. However, the size of the memory address is fixed, so the memory address can be stored in the stack memory. Thus, when querying a variable of a reference type, you can first read the memory address from the stack and then find the value saved in the heap in "Trace". For this kind of query, we call it by reference , because we are not manipulating the actual value, but the object referenced by that value.

To retrieve the differences between basic data types and reference types in 01-c#

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.