Taste types-value types and reference types

Source: Internet
Author: User

Basic concepts

Value type:

A value type instance is typically assigned on the thread's stack (stack) and does not contain any pointers to the execution of the instance data because the variable itself contains its data instance. Its definition in MSDN is that value types directly contain their data, and instances of value types are either on the stack or inline in the structure.

Value types mainly include: simple types, struct types, enumeration types, and so on. Typically declared as a type: int, char, float, long, bool, struct, enum, short, Byte, Decimal, and so on.

Reference type (Reference type):

A reference type instance is allocated on the managed heap (managed heap), and the variable holds the memory reference to the instance data. Its definition in MSDN is that a reference type stores a reference to the memory address of the value on the heap. A reference type can be a self-describing type, a pointer type, or an interface type. Self-describing types can be further subdivided into arrays and class types.

In-Memory allocations

A value type instance is always allocated where it is declared, when declared as a local variable, it is allocated on the stack, declared as a reference type member when it is allocated on the managed heap, and the reference type instance is always allocated on the managed heap.

General rules

The >.string type is a special reference type with a immutability attribute (the same string has only one memory), so each change to a string produces a new string variable in the managed heap. The string type also resets the = = operator, comparing the actual string to the reference address, which is the same as the value type .

Reference types are less performance-related than value types, mainly because of the following:

A. The reference type variable is allocated on the managed heap, the memory release has GC completion, the GC executes, it iterates through all managed heap objects, finds all reachable objects and inaccessible objects by a certain recursive traversal algorithm, and frees the reclaimed memory for inaccessible objects, causing a certain GC heap pressure.

B. The memory allocation process for its attached Members must be completed at the same time

The memory of a value type is not controlled by the GC, and the value type is automatically freed when the scope ends. When dealing with small data situations, you should prioritize using value types.

. Value types are inherited from System.ValueType, and System.ValueType is inherited from System.Object, the main difference being that ValueType overrides the Equals method, which enables comparison of value types by instance values instead of reference addresses.

Primitive type refers to the type that the compiler supports directly. For example: int corresponds to System.Int32.

Type of award, etc.

Reference: http://www.cnblogs.com/qianxingdewoniu/p/3604136.html

Taste types-value types and reference types

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.