C # value type and reference type

Source: Internet
Author: User

1. the reference type and value type

are lightweight and the reference type is 'heavyweight.

the value type stores the object itself, while the reference type stores the memory address of the object

1. memory Allocation

value-type instances generally are allocated on the thread stack, instances of the reference type are allocated on managed heap .

Note: A value-type instance is not necessarily on the thread stack and has a relationship with the defined position. Local variables and method parameters must be on the stack, however, the member variables of a class are always allocated with other data in the class, which may be allocated to the stack.

C # is different from C/C ++, C # is based on.. NET environment, called hosting. I didn't understand it because I learned C language.

for example, variables stored in the heap or stack in C language are not determined by the variable type. Instead, variables are defined. The stack stores the value of local variables, and the system recycles memory allocation. The heap is created and recycled by a programmer.

in C # managed code, the memory storage location of variables is not based on the definition of variables: # ff6666 "> C # The variable type is incorrect, and the object instance is more precise. ) the type is determined.

stack and stack differences between C/C ++

click to open the link

click to open the link.

2. the Net Framework SDK documentation specifies the reference type and value type.

value type: the value type is called structure or enumeration. Struct, int32, Boolean, decimal, and these structures are derived from system. valuetype. All enumerations are from system. derived from Enum

reference type: All classes are reference types


3. initialization

when a value type variable is created, it is a value containing the basic type by default, all members are initialized to 0

when a reference type variable is created, the default value is null, indicating that no valid object is pointed, an nullreferenceexception is thrown.

C # value type and reference type

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.