Simple overview of value types and reference types in C #

Source: Internet
Author: User

Concept:

1. Value type: The data is stored in the stack of memory and can be accessed quickly from the stack, so the value type represents the actual data.

2. Reference type: Represents a pointer or reference (including classes, interfaces, arrays, and strings) that points to data stored in the memory heap.

value types defined in C # include the original type (Sbyte, Byte, short, Ushort, Int, Uint, Long, Ulong, Char, Float, Double, Bool, Decimal), enumeration (enum), struct (struct)

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

Difference:

The basic difference is in how they are stored in memory. Value types store values only in memory, and these value types are stored on the stack. Raw data types, such as bool and int, belong to this type. The memory cell of the reference type only holds the address of the object in the memory heap, and the object itself is placed in the memory heap. If the value of the referenced value type is NULL, then no object is referenced.

Heap and Stack differences:

The heap and stack are two different concepts, and the storage locations in memory are not the same,

Heaps are typically used to store variable-length data, such as String types;

The stack is used to store fixed-length data, such as Integer type data int (each int variable occupies four bytes). Where the data is stored, it is known that when assigning a value variable to another value variable, two identical values are saved on the stack, and a reference variable is assigned to another reference variable, and two references to the same heap location are saved on the stack, that is, the address of the same heap is saved on the stack. In the case of data manipulation, for value types, because each variable has its own value, the operation of one variable does not affect other variables, and for a variable of reference type, manipulating the data of a variable is the operation of the data in the heap, if a variable of two reference types refers to the same object, The actual meaning is that they hold the same address of the heap on the stack, so manipulating one variable affects another variable that references the same object.

Concept:

1. Value type: The data is stored in the stack of memory and can be accessed quickly from the stack, so the value type represents the actual data.

2. Reference type: Represents a pointer or reference (including classes, interfaces, arrays, and strings) that points to data stored in the memory heap.

value types defined in C # include the original type (Sbyte, Byte, short, Ushort, Int, Uint, Long, Ulong, Char, Float, Double, Bool, Decimal), enumeration (enum), struct (struct)

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

Difference:

The basic difference is in how they are stored in memory. Value types store values only in memory, and these value types are stored on the stack. Raw data types, such as bool and int, belong to this type. The memory cell of the reference type only holds the address of the object in the memory heap, and the object itself is placed in the memory heap. If the value of the referenced value type is NULL, then no object is referenced.

Heap and Stack differences:

The heap and stack are two different concepts, and the storage locations in memory are not the same,

Heaps are typically used to store variable-length data, such as String types;

The stack is used to store fixed-length data, such as Integer type data int (each int variable occupies four bytes). Where the data is stored, it is known that when assigning a value variable to another value variable, two identical values are saved on the stack, and a reference variable is assigned to another reference variable, and two references to the same heap location are saved on the stack, that is, the address of the same heap is saved on the stack. In the case of data manipulation, for value types, because each variable has its own value, the operation of one variable does not affect other variables, and for a variable of reference type, manipulating the data of a variable is the operation of the data in the heap, if a variable of two reference types refers to the same object, The actual meaning is that they hold the same address of the heap on the stack, so manipulating one variable affects another variable that references the same object.

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.